I have Class which declares a method but does not implement it. The method is not a virtual function. In the corresponding cpp file I did not find the definition of the same method. All other methods declared in the class were defined.
I compiled the code and it went fine. I was of the impression that cpp must mandate the definition of a declared method.
Appreciate if somebody could elaborate on the same. I am using cl compiler from VS2010.
An abstract method is a method that is declared, but contains no implementation. Abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods.
Yes you can definitely have functions outside of a class.
Declared class inside the main() function in C++ Show activity on this post. In the following program, I have declared class inside the main() function. and It's working fine in G++ compiler. But, If I remove static keyword and compile it, compiler gives an error.
User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a class method object.
Your code will compile but it will give linking errors.
Building an executable of your project involves two stages:
During Compilation the compiler merely translates the source code into object code by verifying the language semantics.
During Linking the linker actually looks up for the definitions of the symbols and creates an executable from multiple object files (created during compilation).
The compiler compiles the source code in each translation unit (.cpp + header files) separately and hence it assumes the definition should be present in some other source file. It is the Linker who tries to find references to the function definitions, and hence the missing definition will be reported by the linker.
Note that the linker needs to link only those symbols which are used by your program,
For ex: If your program declares a function, provides no definition & then never uses/calls the function anywhere, the linker does not need to embed the code for jumping to the address where the object code for the function resides at any function call site.
Given such a scenario the linker will simply never need to look up for the function definition at all. Hence the code will compile and link.
It is a common technique to prevent assignment or copy. If you declare it but not define it, a linking error will occur if you try to use it i.e. prevents people using it inadvertently
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With