aspen100
aspen100

Reputation: 965

Overriding the default constructor/destructor without implementing them

I have the following class:

//myClass.h

myClass{
    int data;
public:
    myClass();
    void foo1(int);
    void foo2();
    ~myClass();
};

//myClass.cpp

#include"myClass.h"
myClass::foo1(int a){
    data = a ;
}

// main
int main(){
    myClass m;
    m.foo1(10);
}

Why is it that we can have a non implemented function (i.e., function prototype in header, but no definition in .cpp file), but not a non-implemented default constructor or destructor?

If I leave the default constructor/destructor unimplemented (like in the example above), why do i get a compiler error?

I'm seeing an undefined reference to myClass::myClass() when i don't implement the constructor, and an undefined reference to vtable for myClass, when i don't implement the destructor. However, if i implement both of these (even with an empty block {}), and leave a method (ex. foo2()) unimplemented, the compiler doesn't complain.

Aren't constructors/destructors essentially just methods in a class? If so, why can I have an undefined method foo2(), but not undefined constructors or destructors?

If someone could help me understand this (and not just " because that's just the way it is" :), that would be greatly appreciated.

Upvotes: 3

Views: 9386

Answers (4)

John Dibling
John Dibling

Reputation: 101494

Why is it that we can have a non implemented function (i.e., function prototype in header, but no definition in .cpp file), but not a non-implemented default constructor or destructor?

Your assertion is false. You can have a non-implemented default constructor. In fact, this is one way to ensure that your class is never implicitly constructed.

If I leave the default constructor/destructor unimplemented (like in the example above), why do i get a compiler error?

Because you are using it somewhere in your code. Re-read your compiler error. It is likely telling you exactly where.

And here it is:

int main(){
myClass m;
m.foo1(10);

The line myClass m; instantiates myClass, using the default constructor.

You may declare but not implement a default constructor and compile clean if you do not use the defaul constructor. Consider:

class Foo 
{
public:
    Foo (int x) {}
    Foo();
};

int main()
{
    Foo f (1);
}

Here there is a declaration for Foo(), but no implementation. The code compiles with no compiler or linker errors. However, this code will not compile clean:

int main()
{
    Foo f;
}

The latter example is what you were trying to do, here:

myClass m;

Upvotes: 3

Mike Seymour
Mike Seymour

Reputation: 254681

Why is it that we can have a non implemented function (i.e., function prototype in header, but no definition in .cpp file), but not a non-implemented default constructor or destructor?

Any function that's used must be defined. Most functions can be left undefined if they're not used.

If I leave the default constructor/destructor unimplemented (like in the example above), why do i get a compiler error?

Because your program creates and destroys an instance of your class, which uses the constructor and destructor; since they're used, they must be defined. You've declared them, so they won't be defined implicitly; therefore, you must define them yourself.

I'm seeing an undefined reference to myClass::myClass() when i don't implement the constructor;

That's because the constructor is used to create the object.

and an undefined reference to vtable for myClass, when i don't implement the destructor.

That's because this compiler generates the vtable in the same translation unit as the destructor (if it's neither implicit nor inline). If you don't declare the destructor but don't define it, you won't get the vtable either.

However, if i implement both of these (even with an empty block {}), and leave a method (ex. foo2()) unimplemented, the compiler doesn't complain.

That's because you don't use the function. If you were to call it, then you'd get an error.

Aren't constructors/destructors essentially just methods in a class?

More or less, yes; but they are special in various ways. The language specification describes them as "special member functions", and has a whole chapter describing their properties.

If so, why can I have an undefined method foo2(), but not undefined constructors or destructors?

To reiterate: you must define anything you use. Constructors and destructors are used if you instantiate the class.

Upvotes: 0

Zac Howland
Zac Howland

Reputation: 15870

The compiler only cares about the code you have declared (which is what your class declarations do). If you declare your constructor, it assumes that you have implemented it somewhere. The linker will attempt to find that implementation when you use it. For a constructor/destructor, you use it when you attempt to create an instance of the class. If they don't exist, you will get a linker error. If you do not use foo2(), and you do not implement, the linker doesn't need to find it, so it will not try to. Thus, you get no linker error and your code will be successfully compiled and linked into the executable.

Upvotes: 1

NPE
NPE

Reputation: 500843

If so, why can I have an undefined method foo2(), but not undefined constructors or destructors?

Because you are using the constructor and destructor, and not using foo2(). Add a call to it, and you'll get a link-time error.

Upvotes: 4

Related Questions