C++ reference rather than pointer
Say I've got a class myClass then I can write a function like
myfunc(myClass &a)
and then in the function use the reference to call a member function like
a.myMemeberFunc()
If I write the function like
myfunc(myClass *a)
then I have to call member functions like
a->myMemberFunc()
All well and good. Now, within a function (a member function of another class, myClass2, as it happens) I want to write something along the lines of
myClass *refToA = &member
where member is a member variable of myClass2 and is of type myClass. From then on in the function I have to use
refToA->myMemberFunc()
Here's the question: I want to be able to write refToA.myMemberFunc() as I could with the reference approach. How do I write the initial declaration and assignment of refToA so I can do this?
TIA
maybe you should provide a constructor that takes a "myClass" argument to initialize it:
but... probably i'm wrong, well give it a try
myClass::myClass(myClass &x){ //and provide operator=() overloaded function this = x;}myClass refToA(&member)refToA.myMemberFunc()
but... probably i'm wrong, well give it a try
Quote:Original post by VerMan
maybe you should provide a constructor that takes a "myClass" argument to initialize it:myClass::myClass(myClass &x){ //and provide operator=() overloaded function this = x;}myClass refToA(&member)refToA.myMemberFunc()
but... probably i'm wrong, well give it a try
Yes, you're wrong. In many ways.
Firstly, that's called a copy constructor. You should write operator=() in terms of the copy constructor and not the other way around, otherwise you will end up potentially dereferencing uninitialized values, or constructing values and immediately overwriting them with new values.
Zerothly, you should never assign to "this". Bad bad bad.
Minusonethly, your argument x is a reference to a myClass object, and you're trying to assign it to this which is of type pointer-to-myClass.
Nope, ToohrVyk provides the answer.
--smw
there is no way to initialize a class member the way ToohrVyk proposes. one should use initialization lists in constructor:
class c1 {};class c2 { c1& c1ref; c1(const c1& ref): c1ref(ref) {} };
Quote:Original post by ToohrVykA a1;A& a2 = a1; // a2 references a1assert (&a1 == &a2);a2.member();
Does the job nicely. Thanks a lot.
Quote:Original post by ToohrVykclass c2 { c1& c1ref; c2(const c1& ref): c1ref(ref) {} };
why without const? your way you won't be able to construct c2 instance from a temporary object, i.e. you won't be able to:
c1 x, y;c2 z(x+y); // oopsconst c1 fun() {...};c2 z(fun()); // oops
Quote:Original post by mbaitoffQuote:Original post by ToohrVykclass c2 { c1& c1ref; c2(const c1& ref): c1ref(ref) {} };
why without const? your way you won't be able to construct c2 instance from a temporary object, i.e. you won't be able to:c1 x, y;c2 z(x+y); // oopsconst c1 fun() {...};c2 z(fun()); // oops
Without const, because you shouldn't be able to construct c2 instance from a temporary. Think of it, c2 will save the reference of such temporary object in it's c1ref member. Bad things can (and will) happen.
Quote:Original post by mbaitoff
why without const?
- The code does not compile with const, because you would be creating a non-constant reference (c2::c1ref) from a constant reference (ref), thus causing a compiler error.
- The compiler has no reason to let it compile, since the policy is to make const-conversions implicit through use of const_cast. Besides, it makes things confusing, since you're passing a constant reference that the instance may modify!
- Even if c2::c1ref was also constant, creating instances of objects that reference temporary values is a Bad IdeaTM, since the lifetime of the temporary value ends before the object is fully constructed — yielding a member variable that cannot be used except in the constructor.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement