Templated class with constructor that takes T* shadow error [duplicate]

22 hours ago 1
ARTICLE AD BOX

why this is considered to be a compiler error.

Because MyClass(a_owner); is treated as if you've written MyClass a_owner; i.e you're trying create an local object(variable) named a_owner of type MyClass using default initialization.

There are 2 problems with this:

First, since you've already declared a parameter named a_owner, you're no longer allowed to create a same name variable in the same scope. To solve this problem you'll need to choose different names for the two. Second, the compiler will not automatically synthesize a default ctor here because you've already provided a parameterized ctor MyClass::MyClass(TOwner*). To solve this issue, you can either manually declare/define a default ctor MyClass::MyClass() or use brace initialization as in MyClass { a_owner };. Note that there are other way to solve this depending upon what you're trying to achieve.

Here is a working demo after solving the above two problems.


Note that you're missinga return statement from your static function. This will lead to undefined behavior.

Richard's user avatar

Read Entire Article