Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing class constructor [duplicate]

Why this code is incorrect?

class Method
{
public:
   Method(decltype(info2) info1);
   virtual ~Method(){}
protected:
  QSharedPointer<info> info2;
};

But this code is correct:

class Method
{
public:
   virtual ~Method(){}
protected:
  QSharedPointer<info> info2;
public:
  Method(decltype(info2) info1);   
};

why place of class constructor is important? I thought that place of definition class constructor isnt important.

like image 696
djkah11 Avatar asked Nov 08 '22 01:11

djkah11


1 Answers

I believe this part of the standard is relevant [basic.scope.class]/1.1:

The potential scope of a name declared in a class consists not only of the declarative region following the name’s point of declaration, but also of all function bodies, default arguments, exception-specification s, and brace-or-equal-initializers of non-static data members in that class (including such things in nested classes).

Note that it only mentions default arguments. So this works since the decltype is referred in a default argument:

Method(QSharedPointer<int> info1 = decltype(info2)())

And this also works since it's inside a body:

Method(<...>)
{
    decltype(info2) info3;
}

However your example does not work because such a placement of a decltype is not covered by the paragraph I quoted, thus the name info2 is considered out of scope.

like image 124
SingerOfTheFall Avatar answered Nov 16 '22 23:11

SingerOfTheFall