C++ Programming
A very basic question. Sorry I graduated from school for too long.
I have a class A with lots of private data members. Class B is derived
from class A, and has its own private data members.
Now I have an object of A (OA), and an object of B (OB) as well. I want
to copy the values of all variables from OA to OB, can I use the
following line to finish the task?
OB = OA;
If I want to copy the values of those common variables from OB to OA,
can I use the following line?
OA = (Class A) (OB);
BTW, I am using Visual Studio 2003.
Thank you in advance.
Johnson
It depends on how you implement operator= for your class(es). However, you will need accessor or friend methods to get to A's private data from B and vise versa.
So really dont you have three classes?
The Base class which holds the common variables.
A which inherits from Base and
B which inherits from Base?
The Base class which holds the common variables.
A which inherits from Base and
B which inherits from Base?
I agree that using a base class to hold the common variables as protected or public can solve the problem, or overloading the = operator might also work (yes there are functions for reading/writing private members), but I am working on an existing software, and I really do not want to change the class structure. So any other method to directly copy the private members from its parent class or derived class?
Johnson
Sorry I seem to have missed
B is a type of A, so maybe this is what you are looking for?
Quote:I have a class A with lots of private data members. Class B is derived:)
from class A, and has its own private data members.
B is a type of A, so maybe this is what you are looking for?
Quote:
A* a;
B b;
a = &b
FYI, I just got a reply saying we can assign the base object to the derived object directly (with all data members), but not vice versa. (see the attach below)
Since we cannot assign a Base to the derived object directly, and we know the derived object has a Base object inside, so can we assign the Base object to the inside Base Object of the derived object? Can we modify the construction function this way.
class Derived : public Base
{
public:
Derived (Base * b)
{ (Base *) this = b};
private:
int x,y,z;
};
Basically I do not know how to get a pointer to the inside Base object. I do not believe (Base *) this will work. Any idea?
Johnson
____________________________________________________________________________
niklasb@microsoft.com wrote:
> Johnson wrote:
> > A very basic question. Sorry I graduated from school for too long.
> >
> > I have a class A with lots of private data members. Class B is derived
> > from class A, and has its own private data members.
> >
> > Now I have an object of A (OA), and an object of B (OB) as well. I want
> > to copy the values of all variables from OA to OB, can I use the
> > following line to finish the task?
> > OB = OA;
> >
> > If I want to copy the values of those common variables from OB to OA,
> > can I use the following line?
> > OA = (Class A) (OB);
>
> Allow me to express this in code, with the names changed to make
> it easier (for me) to remember:
>
> class Base
> {
> int i,j,k;
> };
>
> class Derived : public Base
> {
> int x,y,z;
> };
>
> int main()
> {
> // Ignore the fact that none of these variables are initialized;
> // the point here is just to talk about assignment semantics.
> Base b,b2;
> Derived d;
>
> b = b2; // Ok
> b = d; // Ok
> d = b; // Error!
>
> return 0;
> }
>
> The first assignment is OK because the types are the same. Base
> doesn't define an assignment operator so the default meaning is
> member-wise assignment.
>
> The second assignemnt likewise invokes the compiler-generated
> assignment operator for Base (per the left-hand operand). Of
> course a Base expected for the right-hand operand whereas d is
> a Derived, but that's OK because conceptually a Derived "is a"
> Base (due to public inheritance); it can be implicitly converted
> to a Base and used in places where a Base is required. The effect
> here is to assign the Base part of d to b. In other words, the i, j,
> and k members of d are assigned to the corresponding members
> of b.
>
> The third assignment is an error. Here the compiler looks for an
> assignment operator for Derived (per the left-hand operand). There
> is only the default, compiler-generated assignment which expects a
> Derived as its right-hand operand. But b is a Base. A Derived "is a"
> Base but the reverse is not true. There is no implicit conversion
> Base to Derived (or from reference-to-Base to reference-to-Derived,
> or from pointer-to-Base to pointer-to-Derived) for reasons which are
> obvious if you think about it. Mind you, a class can define
> conversions, e.g., if Derived had a conversion constructor that
> took a Base (or reference to const Base), but that's not the
> case here.
Since we cannot assign a Base to the derived object directly, and we know the derived object has a Base object inside, so can we assign the Base object to the inside Base Object of the derived object? Can we modify the construction function this way.
class Derived : public Base
{
public:
Derived (Base * b)
{ (Base *) this = b};
private:
int x,y,z;
};
Basically I do not know how to get a pointer to the inside Base object. I do not believe (Base *) this will work. Any idea?
Johnson
____________________________________________________________________________
niklasb@microsoft.com wrote:
> Johnson wrote:
> > A very basic question. Sorry I graduated from school for too long.
> >
> > I have a class A with lots of private data members. Class B is derived
> > from class A, and has its own private data members.
> >
> > Now I have an object of A (OA), and an object of B (OB) as well. I want
> > to copy the values of all variables from OA to OB, can I use the
> > following line to finish the task?
> > OB = OA;
> >
> > If I want to copy the values of those common variables from OB to OA,
> > can I use the following line?
> > OA = (Class A) (OB);
>
> Allow me to express this in code, with the names changed to make
> it easier (for me) to remember:
>
> class Base
> {
> int i,j,k;
> };
>
> class Derived : public Base
> {
> int x,y,z;
> };
>
> int main()
> {
> // Ignore the fact that none of these variables are initialized;
> // the point here is just to talk about assignment semantics.
> Base b,b2;
> Derived d;
>
> b = b2; // Ok
> b = d; // Ok
> d = b; // Error!
>
> return 0;
> }
>
> The first assignment is OK because the types are the same. Base
> doesn't define an assignment operator so the default meaning is
> member-wise assignment.
>
> The second assignemnt likewise invokes the compiler-generated
> assignment operator for Base (per the left-hand operand). Of
> course a Base expected for the right-hand operand whereas d is
> a Derived, but that's OK because conceptually a Derived "is a"
> Base (due to public inheritance); it can be implicitly converted
> to a Base and used in places where a Base is required. The effect
> here is to assign the Base part of d to b. In other words, the i, j,
> and k members of d are assigned to the corresponding members
> of b.
>
> The third assignment is an error. Here the compiler looks for an
> assignment operator for Derived (per the left-hand operand). There
> is only the default, compiler-generated assignment which expects a
> Derived as its right-hand operand. But b is a Base. A Derived "is a"
> Base but the reverse is not true. There is no implicit conversion
> Base to Derived (or from reference-to-Base to reference-to-Derived,
> or from pointer-to-Base to pointer-to-Derived) for reasons which are
> obvious if you think about it. Mind you, a class can define
> conversions, e.g., if Derived had a conversion constructor that
> took a Base (or reference to const Base), but that's not the
> case here.
Quote:Original post by JohnsonGPS
Basically I do not know how to get a pointer to the inside Base object. I do not believe (Base *) this will work. Any idea?
Remeber, when dealing with inheritance, the derived object is a base object. It is perfectly legal to do something like this:
B oB; //B is your derived classA oA = oB; //or, explicitely, A oA = static_cast<A>(oB);//oA now holds the base portion of oB
Similarly, you can pass a B object to any function expecting an object of type A. For more information, look up object slicing.
It is also perfectly legal to "down cast" pointers to derived objects:
B *pB = new B;A *pA = pB; //or, A *pA = reinterpret_cast<A *>(pB);
Note that when casting pointers, no slicing occurs.
Quote:Original post by JohnsonGPSclass Derived : public Base
{
public:
Derived (Base * b)
{ (Base *) this = b};
private:
int x,y,z;
};
I don’t even think that’s valid C++, IIRC you can’t modify the this pointer. The this pointer should point to "this" object. What you have there makes it point to some other object.
I think what you want to do is cast this to Bace* and then dereference it, THEN assign it a value.
class Derived : public Base{public: Derived (Base * b) { *((Base*)this) = *b};}
Or even use a the copy constructer.
class Derived : public Base{public: Derived (Base * b) { *((Base*)this) = Base(*b)};}
Try this. It works both way.
class BaseMine
{
int i,j,k;
public:
void Setj(int t)
{j=t;}
int Getj()
{return j;}
};
class DerivedMine : public BaseMine
{
int x,y,z;
};
BaseMine b;
DerivedMine d1;
d1.Setj(100);
b = (BaseMine) d1;
int y = b.Getj();
//It does not work, but can pass the compilor
b.Setj(10);
(BaseMine) d1 = b;
y = d1.Getj();
//It can pass the compiler and works fine.
d1.BaseMine::operator =(b);
y = d1.Getj();
class BaseMine
{
int i,j,k;
public:
void Setj(int t)
{j=t;}
int Getj()
{return j;}
};
class DerivedMine : public BaseMine
{
int x,y,z;
};
BaseMine b;
DerivedMine d1;
d1.Setj(100);
b = (BaseMine) d1;
int y = b.Getj();
//It does not work, but can pass the compilor
b.Setj(10);
(BaseMine) d1 = b;
y = d1.Getj();
//It can pass the compiler and works fine.
d1.BaseMine::operator =(b);
y = d1.Getj();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement