can i make a prototype for a class member function outside of the class declaration?
i tried this: but it didnt work
class cls;
void cls::dosomething();
class cls
{ public:
void dosomething(){}
};
thanks
can i make a prototype for a class member function outside of the class declaration?
i tried this: but it didnt work
class cls;
void cls::dosomething();
class cls
{ public:
void dosomething(){}
};
thanks
no
no you can't
put the prototype inside the class declaration and the implementation outside the class
The class declaration is the prototype of the member function (with optional definition).
Computer languages walk a line between expressiveness, safety, efficiency of both size and speed at runtime, coherency, compilation and linking speed and other things. C++ is strongly typed, so allowing you to "hijack" a class by declaring a new member outside the scope of the class declaration would be very much not C++-like. There are many other reasons why class members must be declared only within the scope of the class.
If you are looking for a language that allows what you are asking for, search for languages that allow monkey patching.
Just to clarify, the prototype is what tells the compiler that the function exists, so if another function calls that function before it is actually defined, the compiler knows it exists and doesn't need to throw an error at you. A class declaration (as you've shown), is the same thing. It includes more than just the class name out of necessity, since when you call the class inside your code, you'll usually call member functions and variables.
So, no, you can't declare a class like that, since it doesn't give the compiler the proper information.
Are you trying to define the implementation outside the class like below?
class SomeClass
{
public:
void doSomething();
};
void SomeClass::doSomething()
{
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.