1

What are the ramifications of assigning a class variable when defining the class versus in the class constructor? Is the variable assigned in the class definition accessible by all class instances?

Example of assignment at instantiation:

class Foo
{
    private:
        int x;
        double y;

    public:
        Foo()
        {
            x = 0;
            y = 1.;
        }
};

Example of assignment in class definition:

class Foo
{
    private:
        int x = 0;
        double y = 1.;

    public:
        Foo();
};

edit: As to the class member being accessible by all instances, I think I was looking for the notion of a static declaration, I guess I'm just new to the curly brace languages.

Carter Canedy
  • 73
  • 1
  • 9
  • I'm not sure what you mean by "accessible by all class instances". – Nathan Pierson Sep 14 '22 at 20:51
  • [C++ - initializing variables in header vs with constructor](https://stackoverflow.com/q/28413154) – 001 Sep 14 '22 at 20:56
  • 3
    Side note: Get familiar with the [Member Initializer List](https://en.cppreference.com/w/cpp/language/constructor). It turns the first example into `Foo(): x(0), y(1.) { }`. This becomes important because all class members and base classes must be initialized before entering the body of the constructor. It's not so important for primitive data types, their default initialization does nothing, but for objects with expensive default initialization that will be undone by a later assignment or types with no default constructor, using the member initializer list is the best (or only) way to go – user4581301 Sep 14 '22 at 20:57
  • One ramification is that the first variant compiles on older c++ compilers such as Turbo C++ and the second variant does not. – Dmytro Sep 14 '22 at 21:04
  • In 2022 having to write code to support a compiler from the 1980s is ... problematic. Yes I know it happens, but that doesn't make it any less problematic. I find it best to treat Turbo C++as a different language; It sits like a hominid sitting between proto-ape and humanity with respect to 1980s C and Modern C++. – user4581301 Sep 14 '22 at 21:08

2 Answers2

1

In this code snippet

int x = 0;
double y = 1.;

there is no assignments. There are initializations.

In this code snippet

Foo()
{
    x = 0;
    y = 1.;
}

there is indeed used the assignment operator.

In general for objects of complex types it can be 1) impossible (either the default constructor or the assignment operator is not available) or 2) requiring many resources because at first default constructors are called that create the objects and after that there are called assignment operators.

It is desirable to define constructors at least like for example

    Foo() : x( 0 ), y( 1 )
    {
    }

using mem-initializer lists.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I'm not sure what you're referring to when you say "it can be 1) impossible or 2) requiring many resources". Are you talking about the notion of shared instance variables? – Carter Canedy Sep 14 '22 at 20:59
  • also, with your constructor, is there a practical benefit to adding a scope that's empty, or is it more syntactical? – Carter Canedy Sep 14 '22 at 21:33
  • @CarterCanedy Do you mean the empty body of the constructor? If so then it depends on how the constructor is complex. – Vlad from Moscow Sep 14 '22 at 21:35
1

Is the variable assigned in the class definition accessible by all class instances?

No!

The difference is that you default-initialize the member variables and then assign values to them in the first case. In the second case you value-initialize them - which is preferred. A third option, that also value-initializes them, is to use the member-initializer list:

class Foo
{
    private:
        int x;
        double y;

    public:
        Foo() : x{0}, y{1.}
        {
        }
};
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108