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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:48:13+00:00 2026-06-07T11:48:13+00:00

Whenever I define a variable and give it a value at the same time

  • 0

Whenever I define a variable and give it a value at the same time inside a class, I get an error. What is the reason for this?

As you can see, this doesn’t work…

class myClass {
    private:
        int x = 4; // error
};

But when I keep the variable undefined it does:

class myClass {
    private:
        int x;
};
  • 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-07T11:48:14+00:00Added an answer on June 7, 2026 at 11:48 am

    Since no one else is using member initialization, I’ll introduce you:

    class myClass {
        private:
            int x;
        public:
            myClass() : x (4){}
    };
    

    It’s always better to use this over assigning in the body of the constructor, since by the time the body begins, all user-defined members will have already been initialized whether you said so or not. Better to do it once and actually initialize the non-user-defined members, and it is the only method that works for both non-static const members, and reference members.

    For example, the following will not work because x isn’t being initialized in the body, it’s being assigned to:

    class myClass {
        private:
            const int x;
        public:
            myClass() {x = 4;}
    };
    

    Using a member initializer, however, will, because you’re initializing it off the bat:

    class myClass {
        private:
            const int x;
        public:
            myClass() : x (4){}
    };
    

    Note also that your int x = 4; syntax is perfectly valid in C++11, where it subs in for any needed initialization, so you’ll benefit if you start using it.

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

Sidebar

Related Questions

Whenever I try installing anything using gem, I get this error - murtaza@murtaza-dev:~$ sudo
I know how to do a global variable, but whenever I try to define
Is there a way to define/undefine debug messages using std::cout whenever inside a program?
I need to define a class like this: class Color { private: union Data
We have an custom error class that is used whenever we throw an exception:
I have a variable that is not supposed to change its value after it's
Currently whenever i am trying to determine when a variable is not defined i
So the short of it is I want to define a global string variable
I have a variable within a loop that i am changing whenever i touch
i am trying to get a value from a key stored on a string

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.