basic declaration question

Class MyClass {

};

int main()
{
MyClass a;
MyClass b();
}

What is MyClass b()? Is it a function declaration?

Thanks.
Yes. It declares a function named b that takes no parameters and returns an object of type MyClass.
As I know it is a function. And If you want to make it work. You shall define a constructor function in your class. For exp.


// C++ is sensitive for the difference of 'A' & 'a' (I don`t how to express this well)
#include <iostream>
using namespace std; // avoid the unconvenient of the type like "std::cout ..."
class MyClass // "Class" is not functioning.(use "class")
{
public: // under public the function & parameter can be used.
	MyClass( int i ); // declaration of the constructor.(also you can overload it by using copy constructor)
private: // under private the function & parameter can`t be seen.(make sure it won`t be used by out members)
	int a; // define a private for this class only. I added it.
};

// the functions of constructor
MyClass::MyClass( int i )
{
	a = i;
}
 int main()
 {
 MyClass b(); // I deleted "MyClass a" as my constructor declaration is not MyClass();(I mean it is not functioning)
 MyClass d( 3 ); // I added this. You can debug here to see the value changes(remember to add a breakpoint)
 }


Add this above in one of you .cpp file and you will see clearer.
@c90a78,
MyClass b(); // I deleted "MyClass a" as my constructor declaration is not MyClass();(I mean it is not functioning)
buddy, what are you trying to say?
I mean if I declare the constructor like
MyClass( int i );

Then the use of
MyClass a;
to define an example of
class MyClass
is invalid.
And if I want to make it valid. Constructor
MyClass();
is needed.

I`m new here & I`m also new in C++. I apologized that I answered your question in such a paradoxical way. (I`m really not good in expressing as English is not my nature language.)
Last edited on
This is known as C++'s "most vexing parse" as coined by Scott Meyers.
@moorecm
Oops! My teacher just introduced me the <More Effective C++>. But I haven`t read it yet as I`m just a beginner.
Topic archived. No new replies allowed.