pointer conversion problems with a copy constructor

This is a discussion on pointer conversion problems with a copy constructor within the C++ Programming forums, part of the General Programming Boards category; Given this class declaration in my CLamp.h file: Code: class CLamp: public CSwitch, public CBulb { public: CLamp(int x = ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    pointer conversion problems with a copy constructor

    Given this class declaration in my CLamp.h file:

    Code:
    class CLamp: public CSwitch, public CBulb
    {
    public:
    	CLamp(int x = 0);				// constructor that will construct the lamp with x watts
    	CLamp(const CLamp &original);		// copy constructor
    	void on(void);						// turn switch and bulb on
    	void off(void);						// turn switch and bulb off
    	void print(void);				// print the state of the lamp
    	int getpower(void);				// return power currently used by the lamp
    	~CLamp(void);					// destructor
    };
    When working on my copy constructor indicated below:
    Code:
    CLamp::CLamp(const CLamp &original)
    {
    	// make a hard copy of CBulb variables
    	setwatts(original.getpower());
    	if(original.getstate())
    	{
    		CBulb::on();
    	}
    	else
    	{
    		CBulb::off();
    	}
    
    	// make a hard copy of CSwitch variables
    	if(original.getswitchstate())
    	{
    		close();
    	}
    	else
    	{
    		open();
    	}
    }
    When I compiled my code, I am having the error about pointer conversion error when i used the original accessor methods to make hard copies of CLamp. Below shows the error message:

    Code:
    --------------------Configuration: lab1_q1 - Win32 Debug--------------------
    Compiling...
    CLamp.cpp
    G:\lab1_q1\CLamp.cpp(23) : error C2662: 'getpower' : cannot convert 'this' pointer from 'const class CLamp' to 'class CLamp &'
            Conversion loses qualifiers
    G:\lab1_q1\CLamp.cpp(24) : error C2662: 'getstate' : cannot convert 'this' pointer from 'const class CLamp' to 'class CBulb &'
            Conversion loses qualifiers
    G:\lab1_q1\CLamp.cpp(34) : error C2662: 'getswitchstate' : cannot convert 'this' pointer from 'const class CLamp' to 'class CSwitch &'
            Conversion loses qualifiers
    Error executing cl.exe.
    Creating browse info file...
    
    lab1_q1.exe - 3 error(s), 0 warning(s)
    I have no idea what is going on with the pointer conversion. Any comments would be much appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    getpower(), getstate(), and getswitchstate() need to be declared as const, because "original" is const.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    After declaring those accessor functions to be const, I have another problem with overloading signatures. I have getpower() function in both CLamp and CBulb classes. How would I specify which getpower() to call?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    By prefixing the function name with the class name and two colons.

    As a rough rule, if you are having to worry about calling an inherited version of a member function in your constructor, then you often have a problem with your class design. Also keep in mind that CLamp's copy constructor will implicitly invoke a CBulb constructor. It would probably be a good idea if you made it invoke CBulb's copy constructor, eg.
    Code:
    CLamp::CLamp(const CLamp &original) : CBulb(original)
    {
        // stuff specific to CLamp copying here that is not copied by CBulb's constructor
    }

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    This is what I tried to do:
    Code:
    setwatts(original.(CLamp::getpower()));

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Please acknowledge that you understand that you are learning inheritance incorrectly by using this example. If you know that you are doing it wrong and choose to do it anyway, then that is your choice, but I (and perhaps we) want to make sure you didn't accidentally gloss over the advice because you didn't see it or didn't understand it.

    (The problem I'm talking about is that CLamp should contain CSwitch and CBulb, it should not publicly inherit from them.)

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I do acknowledge that there is a problem with my class design, so I got rid of the method overloading confusion by renaming one of the member functions. I have also fixed a signature problem that's included in the problem.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The problem is with the use of inheritance. This is a classic example of when not to use public inheritance. Is there a reason you chose to use inheritance in this case?

    An inheritance hierarchy with lamps might be a base class of Lamp, and derived classes for DeskLamp, FloorLamp and PortableLamp. A desk lamp is a lamp, so it makes sense to inherit from it. A desk lamp works like a lamp, so in code that does stuff with a lamp you can use a desk lamp and it should work."Is-a" and "works-like-a" are the relationships you should use for public inheritance.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    This class may not warrant writing a copy-constructor, destructor, or assignment operator at this stage because it contains no member variables of its own.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  3. Template generated copy constructor
    By AverageSoftware in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2007, 10:51 AM
  4. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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