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...)