Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Endure order of construction?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Francesco S. Carta, post: 3950874"] I forgot to add one thing. From the functional point of view, using a const pointer to a non const object is equal to just using a reference, only the syntax used to access those members changes. It means that my example, based on your code, could be rewritten as: ------- #include <iostream> using namespace std; class A {}; class B {}; class Spider { public: Spider(A& a, B& b) : a(a), b(b) { } void print() { cout << "&Spider_.a == " << &a << endl; cout << "&Spider_.b == " << &b << endl; } private: A& a; B& b; }; class BigClass{ public: BigClass() : Spider_(A_,B_) { } void print() { cout << "&bc.A_ == " << &A_ << endl; cout << "&bc.B_ == " << &B_ << endl; Spider_.print(); } private: A A_; B B_; Spider Spider_; }; int main() { BigClass bc; bc.print(); return 0; } ------- Considerations should be made about the fact that using pointers can give a hint about the locality of the data. The code inside of Spider's members will have to use the "->" member access syntax, recalling to the coder that those objects - in this very case - do not directly belong to Spider. But those considerations happen to fall under the "tastes" category, and many - probably most - C++ coders will prefer references over const pointers to non-const objects, also because the member access syntax will be more compact and faster to type. Just another two cents added. Have good time, Francesco [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Endure order of construction?
Top