caesar
caesar

Reputation: 3135

Detecting Default constructor works or not

I have a simple class definition in Foo.h like :

template <typename T>
class Foo
{
  public:
    Foo();
  private:
     char *topPtr;
}

I've implemented Foo.cpp like :

template <typename T>
Foo<T>::Foo(){
    cout<<"default constructor is runned"<<endl;
    this.topPtr=NULL;
    if(topPtr==NULL){cout<<"topPtr is null"<<endl;}
}

Now, to see whether my Stack constructor is run or not, I write a simple main.cpp like :

#include <iostream>
#include "Foo.h"
using namespace std;

int main(){
    Foo<int> foo1();
    return 0;

}

I supposed to see "default constructor is runned" and "topPtr is null" messages on my terminal,however I've got nothing. Is there anyone to help me ? Thanks in advance.

Upvotes: 0

Views: 48

Answers (2)

masoud
masoud

Reputation: 56479

You don't need (), by using it, you're declaring a function named foo1 that returns Foo<int> and takes no parameter.

Foo<int> foo1; // It calls default constructor

 

To use this pointer you should use -> not .

this->topPtr // to dereference this pointer

Upvotes: 1

benipalj
benipalj

Reputation: 704

The statement Foo<int> foo1(); declares a function foo1 which returns Foo<int>. You should be doing: Foo<int> foo1{};

See this: Link

And your this.topPtr=NULL; should be this->topPtr=NULL;

Upvotes: 4

Related Questions