Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8619757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:25:02+00:00 2026-06-12T06:25:02+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:25:03+00:00Added an answer on June 12, 2026 at 6:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why avoid while loops? I am trying to learn Python by following
Possible Duplicate: Java Generics, how to avoid unchecked assignment warning when using class hierarchy?
Possible Duplicate: Avoid resending forms on php pages Index.html <form method=post action=demo.php> <input type=text
Possible Duplicate: Why avoid increment (“++”) and decrement (“--”) operators in JavaScript? I've heard
Possible Duplicate: How to avoid “!= null” statements in Java? Share your thoughts..
Possible Duplicate: why in some scripts they omit the closing php tag ?> As
Possible Duplicate: How to fetch result from MySQL row with multiple same-name columns with
Possible Duplicate: Java floats and doubles, how to avoid that 0.0 + 0.1 +
Possible Duplicate: jQuery Ajax always returns “undefined”? I'm trying to come up with a
Possible Duplicate: My regex is not working properly Suppose I have long text. From

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.