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 8734121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:46:46+00:00 2026-06-13T09:46:46+00:00

So I was learning about classes and I stumbled upon something I found was

  • 0

So I was learning about classes and I stumbled upon something I found was quite awkward to me.

class Nebla 
{
    public:
        int test()
        {
            printout();
            return x;
        }

        void printout()
        {
            printout2();
        }

    private:
        int x,y;
        void printout2()
        {
            cout<<"Testing my class";
        }
};

I found that in a class I can use functions before I declare them (prototype them)

You can see I used printout() , printout2() before decleration.

And I can use variables also before declaring them

You can see I did return x; before declareing x.

Why can I use functions and variables in classes before declaration but outside the class if I do that, I get an error?

Thanks

  • 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-13T09:46:47+00:00Added an answer on June 13, 2026 at 9:46 am

    Good question; I’ve relied on that feature for years without thinking about it. I looked through several C++ books to find an answer, including Stroustrup’s The C++ Programming Language and The Annotated C++ Reference Manual, but none acknowledge or explain the difference. But, I think I can reason through it.

    The reason, I believe, that your example works is that the bodies of your test and printout aren’t truly where they appear in your file. The code

    class MyClass {
      void someFun() {
        x = 5;
      }
      int x;
    };
    

    …which appears to violate the rule of having to declare variables before you use them, is actually equivalent to:

    class MyClass {
      void someFun();
      int x;
    };
    
    void MyClass::someFun() {
      x = 5;
    }
    

    Once we rewrite it like that, it becomes apparent that the stuff inside your MyClass definition is actually a list of declarations. And those can be in any order. You’re not relying on x until after it’s been declared. I know this to be true because if you were to rewrite the example like so,

    void MyClass::someFun() {
      x = 5;
    }
    
    class MyClass {
      void someFun();
      int x;
    };
    

    …it would no longer compile! So the class definition comes first (with its complete list of members), and then your methods can use any member without regard for the order in which they’re declared in the class.

    The last piece of the puzzle is that C++ prohibits declaring any class member outside of the class definition, so once the compiler processes your class definition, it knows the full list of class members. This is stated on p.170 of Stroustrup’s The Annotated C++ Reference Manual: “The member list defines the full set of members of the class. No member can be added elsewhere.”

    Thanks for making me investigate this; I learned something new today. 🙂

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

Sidebar

Related Questions

I'm just learning about classes and objects and Scala, and yesterday I saw something
I'm learning about packages. I have two classes that are in different packages and
I'm still learning about Objective-C memory management. I'm trying to implement several simple classes
Learning about notifyAll made me question something about notify: in a typical situation we
Something I don't enjoy about programming is learning a new API. For example, right
I'm learning about classes in C# and am trying to build a DataAccessClass. I
I am learning about PHP classes. I do have a bit of a C++
Hello I am just learning more about using classes in PHP. I know the
I've been learning about classes in JavaScript, the use of prototype and finally how
i am learning about the difference between abstract classes and interfaces, but theoretical explanations

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.