0

C++ Primer, Lippman et. al. 5th edition § 7.5.3 says,

The default constructor is used automatically whenever an object is default or value initialized. Default initialization happens When we define nonstatic variables (§ 2.2.1, p. 43) or arrays (§3.5.1, p. 114) at block scope without initializers

Tried a test program. Static objects created from user defined class do call default constructors in my test program.

If the highlighed statement is true, static object of user defined class type shouldn't call default constructor, isn't it, but, it does in my case.

Then is my interpretation of the statement wrong, or something else ?

Onkar N Mahajan
  • 410
  • 3
  • 13
  • Statics are zero-initialized. Effectively, this makes a difference for POD types mostly, [see](https://stackoverflow.com/questions/27484483/default-initialization-versus-zero-initialization) – alagner Jan 02 '22 at 11:50
  • 1
    @OnkarNMahajan The quoted statement only talks about `nonstatic` members and you are *mistakenly* extending it for `static` members by saying that *"since it holds for nonstatic members so it must not hold for static members"*. –  Jan 02 '22 at 12:31
  • @AanchalSharma then why authors have used the word nonstatic here ? Can you please explain me the usage of that word in the statement by example ? – Onkar N Mahajan Jan 02 '22 at 14:20
  • @OnkarNMahajan If you want to know what `nonstatic` mean in this context then you should ask a separate question asking exactly that. For starters, you can refer to [this](https://en.cppreference.com/w/cpp/language/data_members). –  Jan 02 '22 at 14:56

1 Answers1

0

If the highlighed statement is true, static object of user defined class type shouldn't call default constructor, isn't it

This(above quoted statement) is not true. You are clearly misinterpreting what the author(Lippman) said. In particular, they never said that static object of user defined type shouldn't call default constructor.

is my interpretation of the statement wrong

Yes your interpretation is wrong, this is because:

The default constructor is used automatically whenever an object is default or value initialized. Default initialization happens When we define nonstatic variables (§ 2.2.1, p. 43) or arrays (§3.5.1, p. 114) at block scope without initializers

The above quoted statement doesn't mean/imply that static data members cannot use default constructor.

For example, consider the following snippet:

#include<iostream>
struct Name
{
    Name()
    {
        std::cout<<"default constructor called"<<std::endl;
    }
    Name(std::string pname): name(pname)
    {
        std::cout<<"converting constructor called"<<std::endl;
    }
    std::string name;
};
struct Test
{
    //lets give this class two static data member
    static Name myName; 
    static Name myAnotherName;
};

//definition of static member in exactly one translation unit 

Name Test::myName; //this will use Name's default constuctor
Name Test::myAnotherName("some name"); //this will use Name's converting constructor

int main(){

    Test t;
    return 0;
}

The output of the above program is:

default constructor called
converting constructor called

As shown in the above example, static data members can be constructed using default constructor. Also, you can even initialize the static data member using some other constructor, say using the converting constructor as shown above.

If you want to know what static data members means you can refer to this and for nonstatic data members you can refer to this .

Jason
  • 36,170
  • 5
  • 26
  • 60
  • I already said that I have tried and default constructor is called when I used static .. so I don't think I have any doubt that default constructer is called during static objects of class type is initialized. Please explain me the usage of word nonstatic here - non-static means anything except static, do you agree with me on this ? – Onkar N Mahajan Jan 02 '22 at 14:22
  • @OnkarNMahajan Your question was and i quote: *"If the highlighed statement is true, static object of user defined class type **shouldn't call** default constructor, isn't it"*. This is not true. You are clearly misinterpreting what the author(Lippman) said/wrote. – Jason Jan 02 '22 at 14:28
  • then please explain me the right interpretation of word non-static. – Onkar N Mahajan Jan 02 '22 at 14:32
  • @OnkarNMahajan Ok so you actually wanted to know the meaning of `static` and `nonstatic`. These keywords are used in a wide variety of situations. For example, you can refere to [this](https://stackoverflow.com/questions/5255954/what-is-the-difference-between-static-and-normal-variables-in-c), [this](https://www.knowledgeboat.com/question/differentiate-between-static-data-members-and-nonstatic-data--26205401031972812). Also, i would recommend you to **ask a separate question** so that its intent is different from this question and these 2 questions don't get mixed up. – Jason Jan 02 '22 at 14:38
  • Ok I will wait for other answers and possibly try to find the answer myself .. Thanks for your help so far .. – Onkar N Mahajan Jan 02 '22 at 14:42
  • @OnkarNMahajan But your current question doesn't say that you want to know the difference between `static` and `nonstatic`. I have answered your question according to what you have actually asked. Usually we should ask **one question per post** on Stackoverflow and be clear as possible. You can mark this question as solved by clicking on the check mark at the left side of my answer and then ask a separate question with the title *difference b/w static and nonstatic* or something like that. And i and others can answer that question. Also, you're welcome. – Jason Jan 02 '22 at 14:48