copy constructor/type converter?

This is a discussion on copy constructor/type converter? within the C++ Programming forums, part of the General Programming Boards category; Hi there, I have two individual classes:class A, and class B How can I convert class A to class B ...

  1. #1
    kny
    Guest

    Question copy constructor/type converter?

    Hi there,
    I have two individual classes:class A, and class B
    How can I convert class A to class B if necessary, vice versa?
    I tried copy constructors in both classes, it won't work.
    I tried overload assignment operator, but this operator accept both arguments with same type only.
    Thank you!

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    .. Eh?

    Code:
    class B;
    
    class A
    {
    public:
    	void set_data(int a)  { data = a; }
    	int get_data(void)   { return data; }
    	void operator = (B t);
    private:
    	int data;
    };
    
    class B
    {
    	int data;
    public:
    	void set_data(int a)  { data = a; }
    	int get_data(void)   { return data; }
    };
    
    void A :: operator = (B t) { data = t.get_data(); }

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Or use friend

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  2. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  3. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 03:21 PM
  4. Quick ? on copy constructor
    By Traveller in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2002, 10:31 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21