80.4k views
1 vote
Is this correct code:

template
class Coordinates
{
...
T getX();
};

T Coordinates::getX()
{ return x; }**

A) Yes
B) No
C) Only if 'x' is a static member
D) Depends on the type of 'x'

User Annabel
by
7.5k points

1 Answer

2 votes

Final answer:

No, the code is not correct. The definition of a member function of a template class outside the class must specify that it is a template and include the template parameters. Assuming 'x' is a non-static data member of Coordinates, it should be defined with the template parameter.

Step-by-step explanation:

The code provided is not correct as it stands because when defining a member function of a template class outside the class definition, you need to specify that it is a template function and also provide the template type parameters. The definition of the getX() method should also include the template parameter as follows:

template
T Coordinates::getX() {
return x;
}

This specifies that getX() is a member of a specialized version of the template class Coordinates with a type parameter T. Additionally, it is assumed that x is a non-static data member of type T contained within the Coordinates class, which is returned by the getX() function.

User Tiffiny
by
8.6k points