'private' and 'protected' have no effect on class constructors

Started by
6 comments, last by WitchLord 6 years ago

Hi, maybe i met a new bug.

Like the code below:


class A
{
	protected A()
	{
	}
}

void main()
{
	A a();
}

There should be a compiler error as the constructor A() is private to main(), but it works ok.

 

Advertisement

i think you didn't call the constructor A(),you just "defined" an 'a' function which returns A.

if you want to use the default constructor,remove the brackets like this
 


class A
{
protected:
    A()
    {
    }
};

int main()
{
    A a;
}

and you will get a compiler error(error C2248 for vs)

See Most Vexing Parse.

Basically: everything that could look like a function declaration is interpreted as such.

Also recommended: The Most Vexing Parse: How to Spot It and Fix It Quickly.

 

I encourage Clang for the most informative Most Vexing Parse error messages:


prog.cc:11:8: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
    A a();
       ^~
prog.cc:11:8: note: remove parentheses to declare a variable
    A a();
       ^~
1 warning generated.

🧙

Thanks for your answers, but there's a misunderstanding that I mean it's maybe a bug of angelscript, not C++.

I tested the code in the test project, which is in file test_constructor2.cpp:


const char *script1 =
"class InternalClass            \n"
"{                              \n"
"	protected InternalClass()   \n"
"	{                           \n"
"		m_x = 3;                \n"
"       m_y = 773456;           \n"
"	}                           \n"
"                               \n"
"	int8 m_x;                   \n"
"   int m_y;                    \n"
"}                              \n"
"class MyClass                  \n"
"{                              \n"
"	MyClass()                   \n"
"	{                           \n"
"      m_c = InternalClass();   \n"
"	}                           \n"
"	void Test()                 \n"
"	{                           \n"
"		Assert( m_c.m_x == 3 ); \n"
"       Assert( m_c.m_y == 773456 ); \n"
"	}                           \n"
"	InternalClass m_c;          \n"
"}                              \n";

The constructor InternalClass() is 'protected' now, and still the same result.

I think the same issue was discussed here recently, see my post and Andreas' answer: 

 

For me it doesn't even compile, it looks like it expects some type after private so it definitely parses it as method definition. But I'm quite behind in AS versions so maybe now it parses but has no effect?


Where are we and when are we and who are we?
How many people in how many places at how many times?

Yes, this is related to the problem that noizex first mentioned. I'm looking into this already.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I have implemented proper support for 'private' and 'protected' constructors now in revision 2485.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement