Sign in to follow this  
JohnsonGPS

C++ Programming

Recommended Posts

JohnsonGPS    109
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

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
JohnsonGPS    109

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Sorry I seem to have missed
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

Share this post


Link to post
Share on other sites
JohnsonGPS    109
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.

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
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 class

A 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.

Share this post


Link to post
Share on other sites
Grain    500
Quote:
Original post by JohnsonGPS

[code]class 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)};
}

Share this post


Link to post
Share on other sites
JohnsonGPS    109
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();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this