Roy
Roy
struct Test {
private:
struct hide {};
public:
struct show {};
};
Roy
You can't access to hide struct from Test space?
Roy
Roy
You just do not yet realize how flexible such a design is and how convenient it is to use.
Pavel
But header only
That is worth mentioning if you're doing header-only library
Roy
You don't need to worry about where you store your data, you don't need to store anything, components do it all and you just need to write NeedComponent::
Pavel
Roy
Roy
Roy
this trick have bug, not remember reason of bug
Roy
and it's work only for .cpp if i'm not mistake
Roy
how the hell do I know that? nice to see that i'm developing
Igor🇺🇦
Roy
safe from whom?
user, this trick have filename bug, I don't remember the details
Igor🇺🇦
Roy
https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL59-CPP.+Do+not+define+an+unnamed+namespace+in+a+header+file
Pavel
I wonder actually what happens if you declare a function like that and will have a static variable inside. Because functions in unnamed namespaces marked as static by default, so a function for each translation unit will be generated
Igor🇺🇦
Roy
I don’t know why you are so fixated on this
Igor🇺🇦
className() = delete; should be enough
Roy
Roy
struct AAA {
~AAA() = delete;
};
AAA obj; // error
auto ptr = new AAA(); // ok
delete ptr; // error
Anonymous
Language:
cpp
Source:
struct a{
a() = delete;
};
int main(){
auto var = new a();
return 0;
}
Errors:
652169966/source.cpp: In function ‘int main()’:
652169966/source.cpp:5:21: error: use of deleted function ‘a::a()’
auto var = new a();
^
652169966/source.cpp:2:5: note: declared here
a() = delete;
^
Note:
cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Roy
Roy
you delete ctor
Anonymous
Anonymous
Instead of AAA you wrote ~AAA
Roy
Roy
ohh yea, sorry, my mistake
Anonymous
I posted the code with what you were saying
Anonymous
And even the pointer fails
Roy
Challenge accepted
Roy
auto ptr = new AAA{};
Anonymous
Language:
cpp
Source:
struct a{
a() = delete;
};
int main(){
a vall();
return 0;
}
Note:
cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
olli
Roy
Roy
You cant create them on stack
Roy
but can use new and aggregate ctor
Anonymous
Language:
cpp
Source:
struct a{
explicit a() = delete;
};
int main(){
a cl{};
return 0;
}
Errors:
385489401/source.cpp: In function ‘int main()’:
385489401/source.cpp:5:14: error: use of deleted function ‘a::a()’
a cl{};
^
385489401/source.cpp:2:14: note: declared here
explicit a() = delete;
^
Note:
cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
Language:
cpp
Source:
struct a{
explicit a() = delete;
};
int main(){
auto ptr = new a{};
return 0;
}
Errors:
756292846/source.cpp: In function ‘int main()’:
756292846/source.cpp:5:26: error: use of deleted function ‘a::a()’
auto ptr = new a{};
^
756292846/source.cpp:2:14: note: declared here
explicit a() = delete;
^
756292846/source.cpp:5:14: warning: unused variable ‘ptr’ [-Wunused-variable]
auto ptr = new a{};
^~~
Note:
cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
So challenge failed
Roy
yea, you won
Anonymous
Anonymous
We all learned 👍
Roy
But how many mistakes would you make before finding the correct implementation?
Roy
I prefer to be explicit in code design
Roy
if class not creatable - delete all ctors
Anonymous
Without the explicit keyword it did work like this
Anonymous
Language:
cpp
Source:
struct a{
a() = delete;
};
int main(){
a ptr{};
return 0;
}
Warnings:
1801301646/source.cpp: In function ‘int main()’:
1801301646/source.cpp:5:11: warning: unused variable ‘ptr’ [-Wunused-variable]
a ptr{};
^~~
Note:
cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
Language:
cpp
Source:
struct a{
explicit a() = delete;
};
int main(){
a ptr{};
return 0;
}
Errors:
456582155/source.cpp: In function ‘int main()’:
456582155/source.cpp:5:15: error: use of deleted function ‘a::a()’
a ptr{};
^
456582155/source.cpp:2:14: note: declared here
explicit a() = delete;
^
Note:
cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
Point proven but useless
Igor🇺🇦
olli
Can anyone explain why this compiles? What constructor is being called here?
It's "Aggregate initialization" and people considered this example a defect, starting with C++20 it won't compile anymore.
Before C++20 an aggregate type was allowed to have "explicitly defaulted or deleted constructors [...]".
Since C++20 an aggregate types has "no user-declared or inherited constructors"
Anonymous
Before C++20 you had to explicit the constructor. {} Would call the standard constructor and would be redirected to the special {} constructor
Anonymous
Without explicit the conversion would happen from a() to a special constructor
olli
Igor🇺🇦
Anonymous
This behavior has been corrected now
Anonymous
Anonymous
Language:
cpp
Source:
struct a{
private:
a(){}
};
int main(){
a ptr{};
return 0;
}
Errors:
269750205/source.cpp: In function ‘int main()’:
269750205/source.cpp:6:15: error: ‘a::a()’ is private within this context
a ptr{};
^
269750205/source.cpp:3:6: note: declared private here
a(){}
^
Note:
cplusplus_gcc assumed, other valid options are cplusplus_clang, visual_cplusplus, you can be more specific next time.
Anonymous
The private constructor manage to do this and bypass the madness above
olli
Anonymous
Sync
I'd like to join
Roshan
Is there some way to write:
player1.setPosition().x = 55;
Roshan
Player1 is an object, but what is setPosition() ? Can we access variables of functions in a class?
Ahmet
Roshan
Roshan
Of x
What is setPosition()