Fog Creek Software
Discussion Board




C/C++ Question

This is school related, but not a homework question.

I am looking at two header files in C++. (This is my first experience with C++ - no C knowledge either)

class Parent
{
protected:
    Parent::Parent( Doc* pDoc = NULL, char* name = NULL );

public:
    virtual void Begin( const Point source, const Point target ) = 0;
};

class Child : public Parent
{
public:
    PointBrush( Doc* pDoc = NULL, char* name = NULL );

    void Begin( const Point source, const Point target );
};

Why is the constructor declaration in the parent class preceded by the class name, while the declaration in the child class isn't? I'm guessing the constructor in the parent class is protected so you can't actually create an instance of the parent class?

I've heard of a book called C++ for Java Programmers by Timothy Budd. Can anyone recommend for/against this book.

jw
Friday, October 03, 2003

It's just programmer laziness. The person who wrote it cut'n'pasted the function definition from the source file and forgot to remove the "Parent::" (which is optional in class declarations and never specified in practice).

Andrew Reid
Friday, October 03, 2003

Yes, the Parent:: is unnecessary.  You should be able to remove it and it will still compile.

A private constructor makes it impossible to new up an instance of the class, or to copy it (if it's a copy constructor).  This is done if you want to control the creation of the object (i.e., it's a singleton), or if you want to implement Foo::createFoo() functions (the named initializer idiom).

A protected constructor does the same thing, but you can derive from it also.

Alyosha`
Friday, October 03, 2003

FWIW, note that the presence of the pure virtual function Begin() will also prevent creation of an object of the parent class. So the protected constructor is not strictly necessary for thet reason.

sgf
Friday, October 03, 2003

Furthermore, what exactly is "PointBrush" in the Child class?

It has no type, so it's not a valid method syntax in ISO C++ (I just checked; GCC 3.2.3 rejects it.)

It is not a constructor, since all these must be named "Child".

Is this source code transcribed correctly?

David Jones
Friday, October 03, 2003

I missed that the first time through...  My guess is, it's a transcription error.
1) Except for the name, the syntax is that of a constructor
2) it has the same signature as the parent constructor
3) it's the first declared member function (common practice)

Those syntax errors are easy to miss sometimes.  For whatever reason, you've copy-and-pasted into a new class, and you forget to "rename" the constructor.  All other signs tell you it's a constructor, but it doesn't compile.  Score a point for Python's "<init>" (IIRC).

Brian
Friday, October 03, 2003

I have the Budd book you mentioned (C++ for Java Programmers).  The book is OK, but not great.

I recommend "Accelerated C++" by Koenig and Moo.

Glade Warner
Friday, October 03, 2003

*  Recent Topics

*  Fog Creek Home