Thread: implicit/explicit? conversion of classes?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    implicit/explicit? conversion of classes?

    this is bothering me for a while...

    what i want to achieve is that the same instance of the inherited class will have the same value of the parent class like so...

    Code:
    class A{
      //ctor
      //dtor
      //cctor
      //ator
       ...
    };
    
    class B : public A{
      //ctor
      //dtor
      //cctor
      //ator
      ...
    };
    
    class C : public A{
      //ctor
      //dtor
      //cctor
      //ator
      
      B * b;
    
      func(){
         //OK... i achieve to have (explicitly?) convert C to A 
         A * a = new C(*this);
         b = new B();
         //Error... how would i copy? A to B
         *b = (B)*a;
    
      }
    
    
    };
    I would like to have class B to have the same values of class A as of those with class C. But i can't copy A to B, it says it could not find a match for B(A). how could i achieve this?

    sorry if my terminologies maybe incorrect about explicit or implicit conversion... i'm still having trouble knowing which is which (as well as object slicing).

    Thanks.

    EDIT: forgot something

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    typecast B to A instead of A to B?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    sorry, but what do you mean?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)
    I would like to have class B to have the same values of class A
    Classes do not have values. Objects of a class have members that have values.
    Code:
    *b = (B)*a;
    What the??!! Can you explain what that is supposed to mean?

    I would like to have class B to have the same values of class A as of those with class C.
    Totally incomprehensible!

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    ok...

    let's add something on the sample code:

    Code:
    class A{
      //ctor
      //dtor
      //cctor
      //ator
      int myvar; //assume myvar is actually public for simplicity
    };
    
    class B : public A{
      //ctor
      //dtor
      //cctor
      //ator
      ...
    };
    
    class C : public A{
      //ctor
      //dtor
      //cctor
      //ator
      
      B * b;
    
      func(){
        //i assigned myvar to 1
        myvar = 1; 
        //now what i want is that class B has the same myvar value as it was on class C
        //which is 1.
         
         //this is my try but it didn't work.
         //OK... i achieve to have (explicitly?) convert C to A 
         A * a = new C(*this);
         b = new B();
         //Error... how would i copy? A to B
         *b = (B)*a;
    
      }
    
    };
    sorry if i caused you trouble.

    I would like to have the objects of class B that is derived from class A has the same values as the objects of class C that is derived from class A.

    i want objects of class A for both class B and C to have the same values.

    about this

    Code:
    *b = (B) *a;
    i tried to copy the value of a to b. Hoping that typecasting a to class B will solve incompatibilty problems... but it didn't work...

    thanks!

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    The answer is really staring you in the face
    //cctor
    Create a copy constuctor for B that takes an A object(or pointer)
    Code:
    #include <iostream>
    using namespace std;
    
    class A{
      //ctor
      //dtor
      //cctor
      //ator
    public:
      int myvar; //assume myvar is actually public for simplicity
    };
    
    class B : public A{
      //ctor
      //dtor
      //cctor
    public:
    	B(){}
    	B(A* a):A(*a)
    	{}
      //ator
    };
    
    class C : public A{
      //ctor
      //dtor
      //cctor
      //ator
      
    };
    
    int main () 
    {    
    	
    	A * a = new C();
    	a->myvar = 1;
    
    	B* b = new B(a);
    
    	cout << "a->myvar = " << a->myvar << "\n";
    	cout << "b->myvar = " << b->myvar << "\n";
    }
    This way you have more control over the copy process.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hoping that typecasting a to class B will solve incompatibilty problems
    In C++, type casts are written in such a way that it is clear exactly what you are doing:
    Code:
    double num = 10.5;
    int a = static_cast<int>(num);
    In addition, you can't cast across a hierarchy like this:

    Code:
        base A
         /   \
        /     \
       B------>C
    Last edited by 7stud; 01-31-2006 at 01:13 PM.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    Ok thank you. i'll try adding a copy constructor that takes A object as an argument...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM
  5. conversion from different classes
    By xion in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2004, 09:07 AM