-5

Hello everybody could anyone explain to me the difference between class temlplate member functio return type of type T (parameterized type) and an object of the claas return type

template<class T>
class array
{
public:
     array(T tx): tObj(tx){}
     T getObj()const{return tObj;}
     array GETTOBJ()const;
private:
     T tObj;
}

I have confusion: what is the difference between the return value of the functions getObj() and the GETTOBJ()?????

Raindrop7
  • 3,889
  • 3
  • 16
  • 27

2 Answers2

4

You've got:

  • an array
  • which holds tObj

The tObj's real value is given through the arrays' constructor, and the array remembers it.

The getObj method returns the tObj, the thing that array remembered.

The GETOBJ method returns an array. It's code is not shown, but the difference is already here: it returns some array, not the thing the array remembers.

It's a difference like "returns a candy from a box" and "return the box".

Btw. I think that your sig is missing type parameter. I mean:

not: array GETTOBJ()const;
but: array<T> GETTOBJ()const;

because array is a template, and array without type parameters is meaningless.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
0

In the folowing example "class T" is not defined so, then when initializing this class, you can add as parameters what ever you what. For example:

array <int> _array_var(); //OR
array <char> _array_var(); // OR WHAT TYPE YOU WHANT, EVEN SOME STRUCTURES OR CLASSES

in your example "T tOBJ()" will return something of the type you initialized. For example:

if you

array <int> _array_var(); // THAN T=int.... SO T tOBJ will return int type, equivalent to

int tOBJ();

and so on... class T = typename T

for more info check:

http://en.wikipedia.org/wiki/Template_(C%2B%2B)

ow yeah... and GETTOBJ returnes type of the class... always... the can be equal GETTOBJ with tOBJ in case you define:

array <array> _array_var();
coceban.vlad
  • 509
  • 1
  • 7
  • 23