0

Suppose I define the following class:

class A
{
public:
    int x;
};

Now I can initialize it like a struct:

A a { .x = 1; }

However, when I define any virtual function, the constructor goes away:

class A
{
public:
    int x;
    virtual int f() {}
};

A a { .x = 1; }

The message is:

# error: no matching function for call to ‘A::A(<brace-enclosed initializer list>)’

Why does a virtual function delete that constructor?

haael
  • 972
  • 2
  • 10
  • 22
  • That initialization isn't C++. Anyway, I think you mean brace enclosed initialization, so I closed as duplicate. If you mean something else let me know and I'll consider re-opening. – juanchopanza Aug 30 '15 at 19:22
  • 1
    @juanchopanza It's a GCC extension and not the same thing as the question you linked. – Captain Obvlious Aug 30 '15 at 19:24
  • @CaptainObvlious RIght, so not C++. In which case the answer should be in the gcc documentation. As I said earlier, I am happy to re-open this if necessary. – juanchopanza Aug 30 '15 at 19:26
  • Wrongly closed as dup. Too many questions are closed as dup that aren't dup. – curiousguy Sep 08 '15 at 04:21

0 Answers0