2

Possible Duplicate:
Is it possible to avoid repeating the class name in the implementation file?

I've been working on trying to (re)learn c++ and I am having some comprehension issues with c++ object oriented programming. I've spent a lot of time recently working in Java and perhaps that has made me lazy, but here are the problems I am having.

Here is the not-entirely-complete code with which I am working:

class Pong{
    public:
        Pong();
        void run();


    private:
        //Class to represent a pong ball
        class Ball{
            public:
                Ball();
                int getVector();
            private:
                int xvel,yvel;
        };

        //Class to represent a pong paddle
        class Paddle{
            public:
            private:
        };

        bool init();
        //The ball and player paddles
        Ball ball;
        Paddle p1;
        Paddle p2;
};
//Ball members
Pong::Ball::Ball(){

}
//Paddle members
Pong::Paddle::Paddle(){

}

//Pong members
Pong::Pong() : ball(Ball()),p1(Paddle()),p2(Paddle()){
    ball = Ball();
    p1 = Paddle();
    p2 = Paddle();
}
bool Pong::init(){
    if(!log("Starting pong...")){return 2;}
    return 0;
}
void Pong::run(){
    if(Pong::init() != 0){log("Failed to initialize Pong!");}
    return;
}

The issues I have here are really about readability. I am wondering if there is a less ugly looking way of defining members and nested member members without having to specify something like, for instance, Pong::Ball::Ball(), IE putting the namespace in front of every single member.

Am I doing something wrong here? Coming from java where all members are defined inside the class itself, this seems really... crude to me, perhaps, to define everything (including nested class methods) outside of the class definition. It works, but I cannot help feeling like there is a better way to do it, yet the resource I have been using (cprogramming.com) says this is the way it needs to be done.

(Edit: Just before someone catches me, I know I can remove the duplicate definitions inside the Pong constructor. I just haven't yet...)

Community
  • 1
  • 1
Arialth
  • 157
  • 1
  • 2
  • 8
  • 1
    Those are **not** namespaces, but nested classes. Namespaces are opened with the `namespace` keyword, and you don't need the qualification if the definition is within the namespace braces. – David Rodríguez - dribeas Oct 04 '12 at 14:25
  • Perhaps, but I looked at the answers and they don't seem to provide the same ... information I am looking for. – Arialth Oct 04 '12 at 14:35

1 Answers1

3

Coming from java where all members are defined inside the class itself, this seems really... crude to me, perhaps, to define everything (including nested class methods) outside of the class definition.

That's a non-issue. C++ is so awesome that it lets you define methods both inside and outside of the class definition.

If you want to keep the definition outside, no, there's no way to reduce the qualifications.

I'm only pointing the following out because I think you might have it wrong:

Pong::Pong() : ball(Ball()),p1(Paddle()),p2(Paddle()){
    ball = Ball();
    p1 = Paddle();
    p2 = Paddle();
}

should be

Pong::Pong(){
}

i.e. - no need for any explicit initialization.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Well, I have honestly just been following what I have been told in the tutorials over on cprogramming.com. Is there no difference between defining them on the inside and outside? – Arialth Oct 04 '12 at 14:26
  • If all the member types have constructors, then both are equivalent and so is not providing the constructor at all (which involves *less* coding). If any of the members is a POD type then they are not equivalent. That is, prefer to either explicitly initialize: `Pong::Pong() : ball(), p1(), p2() {}` (if any of the members is POD) or avoid providing the constructor altogether – David Rodríguez - dribeas Oct 04 '12 at 14:29
  • @DavidRodríguez-dribeas yes - in his example - `Ball` is not a POD type and `Paddle` is empty. – Luchian Grigore Oct 04 '12 at 14:31
  • Roger. So. I am safe to place all method definitions inside the class {}; itself? And what exactly is the difference if a member is a plain old data type? I should make sure to specify it in the corresponding initialization list? – Arialth Oct 04 '12 at 14:32
  • @Arialth if it's a POD member, it will not be initialized by default. For example, the `Pong` constructor I wrote won't initialize `Paddle` (which is fine in this case, since it's empty) but will initialize `Ball` (because it's not a POD type). And yes, you can put definitions inside the class if you prefer. – Luchian Grigore Oct 04 '12 at 14:34
  • @Luchian Paddle is a nested (member) class of Pong, though. What makes it different? Just because it is currently empty? I'll be defining and fleshing it out in the future. – Arialth Oct 04 '12 at 14:36
  • @Arialth then value-initialize it `Pong::Pong() : p1(), p2()` if it will remain POD, or provide a constructor and keep just `Pong::Pong()`. – Luchian Grigore Oct 04 '12 at 14:37
  • Alrighty then. I appreciate the help! – Arialth Oct 04 '12 at 14:37