15

I am curious to know that why class' data members can't be initialized using () syntax? Consider following example:

#include <iostream>
class test
{
    public:
        void fun()
        {
            int a(3);
            std::cout<<a<<'\n';
        }
    private:
        int s(3);    // Compiler error why???
};
int main()
{
    test t;
    t.fun();
    return 0;
}

The program fails in compilation & gives following errors.

11  9 [Error] expected identifier before numeric constant

11  9 [Error] expected ',' or '...' before numeric constant

Why? What is the reason? What the C++ standard says about initialization of class data members? Your help is greatly appreciated. Thanks

4
  • 2
    "that why class' data members can't be initialized using () syntax?" Because the language standard says so? Feb 24, 2015 at 13:07
  • 2
    @πάνταῥεῖ: Maybe there should be a language-lawyer tag here as well; I feel like the question is more: why would the standard explicitly disallow this practice when it is allowed everywhere else?
    – wolfPack88
    Feb 24, 2015 at 13:11
  • 3
    The fact that there are several different initialization syntaxes, each of which only works in certain places, is why uniform initialization syntax was developed for C++11.
    – dlf
    Feb 24, 2015 at 13:11
  • 2
    @ArunA.S: Irrelevant comment. This may be achieved as-is, just by changing ( for { and ) for }. No need to refactor everything. Feb 24, 2015 at 13:17

1 Answer 1

20

Early proposals leading to the feature's introduction explain that this is to avoid parsing problems.

Here's just one of the examples presented therein:

Unfortunately, this makes initializers of the “( expression-list )” form ambiguous at the time that the declaration is being parsed:

struct S {
    int i(x); // data member with initializer
    // ...
    static int x;
};

struct T {
    int i(x); // member function declaration
    // ...
    typedef int x;
};

One possible solution is to rely on the existing rule that, if a declaration could be an object or a function, then it’s a function:

struct S {
    int i(j); // ill-formed...parsed as a member function,
              // type j looked up but not found
    // ...
    static int j;
};

A similar solution would be to apply another existing rule, currently used only in templates, that if T could be a type or something else, then it’s something else; and we can use “typename” if we really mean a type:

struct S {
    int i(x); // unabmiguously a data member
    int j(typename y); // unabmiguously a member function
};

Both of those solutions introduce subtleties that are likely to be misunderstood by many users (as evidenced by the many questions on comp.lang.c++ about why “int i();” at block scope doesn’t declare a default-initialized int).

The solution proposed in this paper is to allow only initializers of the “= initializer-clause” and “{ initializer-list }” forms. That solves the ambiguity problem in most cases. [..]

6
  • What would be the result of this class? class Foo { Foo() : x = 1 { } int x(2); }; Feb 24, 2015 at 13:33
  • @GoswinvonBrederlow: Depends. What hypothetical syntax rules does it follow? Feb 24, 2015 at 14:28
  • @LightnessRacesinOrbit: What does it mean that type j looked up but not found?
    – Destructor
    Feb 25, 2015 at 6:28
  • @meet It means the compiler will try to find a type called j in your program, and it will fail. Feb 25, 2015 at 11:51
  • @Lightness Races in Orbit: It follows the hypothetical rules from the question. The point I tried to raise is that it would be confusing when x is initialized twice. But initializing in the constructor is needed when different constructors use different values. So to make it sane the thing to leave out is initializing in the body of the class. Mar 5, 2015 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.