Q: How many classes will the following code generate? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I find this question tricky. What do u ppl think?
template <typename T> class myTemplate
{
public:
T val;
...
};
void myFunction()
{
MyTemplate<int> a;
MyTemplate<double> b;
}

You are instantiating the template twice with two different template parameters, so it'll create two class instances.

Related

What does the angle bracket syntax mean in a function call? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What does this syntax mean?
ForExample<Something>();
Can someone explain with a few examples how and what for it can be used?
This code could be a call of the template function or instantiation of temporary class object.
For example, function is defined as:
template <typename T>
void ForExample(){
// Do something
}
This function can be called as:
ForExample<int>();
In your case type Something can be any type (int, double, string, float ...)
Or we can define template class:
template <typename T>
class ForExample {
// something
};
Temporary object can be created as:
ForExample<int>();

How are variadic arguments treated by templates? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Consider the following function:
void f(...);
And the following structure:
template <class T>
struct s;
template <class R, class...A>
struct s<R(A...)>
{
//...
};
How exactly does s<decltype(f)> get treated, considering its variadic argument type?

How to call base class virtual function in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Suppose I have two classes class A and class B, class B is derived from class A public. Here class A have virtual emp(),and class B have emp(),
In this case how can I call base class virtual function?
You can invoke A::emp() directly
B* obj = new B();
b->A::emp();
Or within a method of A or B.
void B::SomeOtherMethod()
{
A::emp(); // same as this->A::emp();
}

How to assign protected static member of struct in c? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
code is
struct {
protected:
static int labelCounter;
};
protected members can be assigned in methods that belong to the class, or any derived class.
Your static member can be initialized normally, but you have to give a name to the struct:
struct MyStruct { protected: static int labelCounter; };
// .cpp:
int MyStruct::labelCounter = 12;

Using generic template member in a class C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating class that has a templated object (Item<T>) as a member, basically like this:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<T>> vec;
};
Currently, I have Item<string>, but I need to be able to use it with non string objects. Is there a way to do this without templating myClass (which would obviously be a lot of work for a complicated class)?
If your class will only use Item< string>, you may try:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<string>> vec;
};
But if you want any other type of Item in the vector, the answer is No, there is no magic solutions.