Instances of class

May I know why's the answer 3 and not all? I guess I do not quite understand. Could someone explain to me? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Given the following code:
template <typename T = int, 
          typename P = float, int S = 10>
class Foo {
    T items[S];
    P value;
};
int main() {
    Foo<int, float, 20> s1;   
    Foo<int, double, 1> s2;   
    Foo<int> s3;   
    Foo<> s4;
    return 0;
}
How many instances of the class Foo are generated during compilation? 
correct answer is 3.
Not sure if this is a trick question or not. Foo is not a class, it's a class template, so I'm tempted to answer 0.

During compilation? Are they asking how many times the class template Foo is instantiated (not how many objects are created but how many classes are generated from Foo)? In that case the answer is 3.

1. Foo<int, float, 20> (line 9)
2. Foo<int, double, 1> (line 10)
3. Foo<int, float, 10> (line 11 & 12) *

* Both Foo<int> and Foo<> means Foo<int, float, 10> thanks to the default template arguments.
Last edited on
In my opinion, the question is a bit poorly worded.

Foo is not a class. Foo is a class template. A class template is something that the compiler uses to generate classes at compile-time.

______________________________________________

Now, for what your instructor is actually looking for:

On lines 9-12 tell the compiler what classes it needs to produce based off the template.

3 unique classes are generated, and 4 objects are generated. 2 of those objects are the same type (s3 and s4).


Foo<int, float, 20> creates a new class with all 3 characteristics specified.
Foo<int, double, 1> creates a new class with all 3 characteristics specified.
Foo<int> creates a new class equivalent to Foo<int, float, 10> due to the default template arguments specified in the class template.
Foo<> uses the existing class equivalent to Foo<int, float, 10> due to the same reason as above.

The key is this:
template <typename T = int, typename P = float, int S = 10>
If no template arguments are given, it uses the default ones, from left to right.
Similar to how default arguments work for functions.


Remembers back the time when a college test asked what the value of a pointer was, and I gave the address.
Last edited on
Ganado wrote:
a college test asked what the value of a pointer was, and I gave the address

The address of the pointer or the address stored inside the pointer? Wouldn’t the latter be a good answer?

P.s. playpro10, sorry for hijacking your thread.
The address of the pointer or the address stored inside the pointer?

I meant, the question asked for the value of the pointer. I gave it the literal value of the pointer, which is of course either nullptr/NULL, or an address. The test wanted the dereferenced value... (Sorry to veer the thread off further)

I should have just written both values to have cleared up any possible confusion. All in the past though :P
Last edited on
Ganado wrote:
The test wanted the dereferenced value

Well, I would have fallen in the same trap; even now, if someone asked me the value of a pointer, I wouldn’t answer the value of the pointed variable.
Is that a standard, to call the value of the pointed variable the “value of the pointer”? (I hope my question makes sense in English.)

Thanks for your answer.
Is that a standard, to call the value of the pointed variable the “value of the pointer”
Your question makes sense, I would say "No", it isn't standard. If a professional were actually talking about debugging some issue involving pointers, hopefully the technical jargon used would be more concise than what was on a hastily made college test.

I luckily don't need to talk about pointers often, I suppose I would say "points to [value/object]" to reference to the dereferenced object, and saying "value of the pointer itself" when referring to the actual address being dereferenced, perhaps with "itself" being emphasized to disambiguate it.

Or more commonly, I would avoid the word "value" in the first place, and say "the pointer is null", or that it's a "garbage pointer"/"pointing to invalid data" since the address itself that's stored in the pointer usually doesn't matter (e.g. if it's address 0xFFF1 vs 0xFFF2).
Last edited on
Topic archived. No new replies allowed.