assignment constructor overloading

This is a discussion on assignment constructor overloading within the C++ Programming forums, part of the General Programming Boards category; I have two class A and B and B inherits from A, in the class A I have defined: Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    assignment constructor overloading

    I have two class A and B and B inherits from A, in the class A I have defined:

    Code:
    A& A::operator=(const B& b){
      //some code here
    }
    in my main I have

    Code:
    A a;
    B b;
    a = b;
    when I tried to compile all of this it gives an error saying that A needs a

    Code:
    A& A::operator=(const A& a)
    why????

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Weird design, but whatever. It should compile, I believe. Post an example that actually compiles and reproduces the error.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I guess it works now, when I compile it again... my only problem now is this:

    I have a method called print and printit. A and B each has a method called print and printit is in main

    Code:
    void printit( A& a )
    {
     a.print( );
    }
    Here's part of main's code
    Code:
    B b1, b2(2);
    b1 = b2;
    A a; a = b1;
    b1.print( ); a.print( );
    it would print:
    2
    0

    my confusion is why when doing a.print() a is 0, shouldn't it be 2 as we already assign a to b1? and b1 has a value2? how can I make it so that it has a value 0?
    Last edited by -EquinoX-; 10-27-2009 at 07:32 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you slice off the value 2 when you copied b1 to a?

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why would the base class need to know anything about the derived class?
    There must be a better way to design it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by tabstop View Post
    Did you slice off the value 2 when you copied b1 to a?
    Can you give me an example of that? And how to fix this?
    Last edited by -EquinoX-; 10-27-2009 at 10:51 PM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,809
    Quote Originally Posted by -EquinoX-
    Can you give me an example of that?
    Consider:
    Code:
    #include <ostream>
    
    class A
    {
    public:
        virtual ~A() {}
    
        virtual void print(std::ostream& out) const
        {
            out << 0;
        }
    };
    
    class B : public A
    {
    public:
        explicit B(int x) : x(x) {}
    
        virtual void print(std::ostream& out) const
        {
            out << x;
        }
    private:
        int x;
    };
    
    #include <iostream>
    
    int main()
    {
        A a;
        B b(1);
        a = b;
        a.print(std::cout);
        std::cout << std::endl;
    }
    Since a is an A, it cannot possibly have a member variable named x since no object of actual type A has such a member variable. a.print(std::cout) must then print 0, since it results in A::print(std::ostream) being invoked. If it could result in B::print(std::ostream) being invoked, then what should x be?

    Quote Originally Posted by -EquinoX-
    And how to fix this?
    So, the problem is that the parts of b specific to B are "sliced off" when b is assigned to a. If your intention is to use polymorphism, then you should do something like:
    Code:
    int main()
    {
        A* a;
        B b(1);
        a = &b;
        a->print(std::cout);
        std::cout << std::endl;
    }
    since such polymorphism in C++ works through pointers and references.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    is there a way to fix this without changing the main

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,809
    Quote Originally Posted by -EquinoX-
    is there a way to fix this without changing the main
    Probably not, at least not without hacks that will likely over-complicate your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    when we assign a to b a doesn't have the value that b has, is this the problem?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    when we assign a to b a doesn't have the value that b has, is this the problem?
    If by that you mean "a is missing some of the member variables of B (since it is not a B, but an A)", then yes that is most likely the problem.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,809
    Quote Originally Posted by -EquinoX-
    when we assign a to b a doesn't have the value that b has, is this the problem?
    Yes. More generally, the part of B that is specific to B will be sliced off.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    in this code, how can I make so that when I call printit(b2) it prints a 2 instead of 0?

    Code:
    void printit( A& a )
    {
     a.print( );
    }

    Code:
    B b1, b2(2);
    b1 = b2;
    A a; a = b1;
    b1.print( ); a.print( );
    when I do printit(b2) it uses the print method that A has and so it prints a value of A which is 0... which is not right
    Last edited by -EquinoX-; 10-28-2009 at 10:49 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,809
    If this is with respect to my definitions of A and B in post #7, then it is as simple as:
    Code:
    printit(b2);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, here's a silly question: how are A and B defined? When you construct b2 with that 2, where does it go? Does an A have a corresponding variable? How did you define your operator=?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

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