1

I am trying to understand some C++ code and find that my C++ is a little rosty. Amongst others the code features the following class structure (CLASS1 to CLASS3 not shown for brevity):

class CLASS4
  :public CLASS3
  ,public CLASS2{
  public:
    CLASS4(double VARA, double VARB, char VARC, int VARD, double VARE, std::vector<double> VARF, std::string VARG = "")
    throw(Error);
    CLASS4(const CLASS4&);
    ~CLASS4();

    double METHOD1();

  protected:
    void METHOD2();
};

CLASS4::CLASS4(double VARA, double VARB, char VARC, int VARD, double VARE, vector<double> VARF, string VARG) throw(Error)
  :CLASS1(VARC, VARD, VARE, VARF, VARG)
  ,CLASS2(VARB)
  ,CLASS3(VARA, VARC, VARD, VARE, VARF, VARG){}

CLASS4::CLASS4(const CLASS4& VARH)
  :CLASS1(VARH), CLASS2(VARH), CLASS3(VARH){}

CLASS4::~CLASS4(){}

I understand the object concept and the concept of inheritance. I see the constructor and destructor of CLASS4. What I do not understand is the listing of classes in the inheritance :public CLASS3, public CLASS2. Whats the purpose of it? Furthermore I wonder what the throw(Error) is supposed to do. Also I wonder what the purpose of the second constructor CLASS4(const CLASS4&); is. I am aware that this will be a simple problem for a C++ programmer.

7
  • 1
    class T : public U, public V is multiple inheritance. throw-specifications are deprecated and you should not use them. T(T const&) is a copy constructor. Also see The Definitive C++ Book Guide and List.
    – user142019
    Dec 7, 2012 at 12:44
  • Whats deprecated in them (bad practice?). I am looking at this code to find memory leaks. Could it be related?
    – highsciguy
    Dec 7, 2012 at 12:46
  • Can you say what it is supposed to do anyway?
    – highsciguy
    Dec 7, 2012 at 12:48
  • 1
    if you are looking for memory leaks, make sure that the destructors of the base-classes are virtual. Dec 7, 2012 at 12:48
  • 1
    They allow you to explicitly tell what types of exceptions a function can throw. You can still use noexcept (which is not deprecated) to indicate a function does not throw any exceptions.
    – user142019
    Dec 7, 2012 at 12:48

3 Answers 3

2
class CLASS4
  :public CLASS3
  ,public CLASS2

is multiple inheritance. The puspose of multiple inheritance is making your object's functionalities has also functionalities of CLASS2 and CLASS3 which is created from CLASS4 ( read this page for detailed explenation : http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr134.htm )

throw(Error) is supposed to throw you Error when it finds an error, it may sound lame, but it's used for exception handling :) (I suggest you to read this page for clear referance : http://msdn.microsoft.com/en-us/library/6dekhbbc(v=vs.80).aspx)

and CLASS4(const CLASS4&); is the copy constructor. "A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object." (quoted from wikipedia. http://en.wikipedia.org/wiki/Copy_constructor)

2

Listing multiple classes in the base specifier list allows a class to inherit from multiple other classes. This is commonly known as multiple inheritance.

Placing throw(error) after a function declaration is a way for the function to express which exceptions it might throw. It is known as an exception specification. In this case, the CLASS4 constructor with many arguments is declaring that it might throw an error exception. These exception specifications are now deprecated in C++11.

Constructors of the form T(const T&) are known as copy constructors. They are invoked in situations like the following, when an object is being created by copying another one:

T first;
T second(first);

Your CLASS4 class has a copy constructor. It is expect that after the copy constructor has been used to construct an object, the new object will be a copy of the old object.

2
class CLASS4: public CLASS3, public CLASS2

is saying that CLASS4 inherits the public methods and properties of CLASS3 and CLASS2, and treats them much the same as its own public methods (i.e. they are public methods of CLASS4). Had you used private CLASS3, protected CLASS2, you would have taken the public methods of CLASS3, and made them private within CLASS4. Likewise, CLASS2's public methods would become protected in that example. This can be useful within certain situations: say you have a class that can be serialised to disk and drawn to the screen in a meaningful way. You'd inherit "Serialisable" and "Renderable" to gain the properties of both.

The CLASS4(const CLASS4& VARH) constructor is known as the "copy constructor". Given an example of a CLASS4, make me a new CLASS4 that has all the same values in its properties. By default, C++ gives you a simple one that just copies each property in turn, hence you don't usually need to specify. If you're doing something clever (like you assign a big block of memory and you want to also copy the contents of that memory rather than just the pointer to it), you need to manually specify your own that does the clever things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.