Using generic template member in a class C++ [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 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.

Related

qt class using new return non-pointer [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
Seeing that QT has a piece of code like this, how can new return an object instead of a pointer?
You made the wrong conclusion. new always returns a pointer. You need to read the documentation of the classes you are using to know how their constructor works. One way to enable foo f = new foo; is the following:
struct foo {
foo(foo*){}
foo(){}
};
int main(){
foo f = new foo;
}
Note that = here is not assignment! The object f is initialized by calling its constructor taking a pointer to a foo.

Q: How many classes will the following code generate? [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 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.

C++ difference between : , :: operators [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
I new to programming. In stack overflow i couldn't see difference between : & :: is mentioned. Could anyone can explain in detail it helps to beginner learners like me. Thank you.
So you would use :: when you're defining/using methods from a class, so like for example
class foo{
public:
int bar;
int hi(int x);
int func(); // static member function
Foo(int num): bar(num) {}; // use of a colon, initialization list
};
int foo::hi(int x){
//define the function
}
Also if you have static member functions, you can just call those whenever through using foo::func(). You can find more about static member functions online.
The single colon is for member initialization list (you can look this topic up online) where you can initialization member variables in the construction of your class.
You can also find single colon used in polymorphism, when you derive a class from a base class. You can find more information about c++ polymorphism online.

When to use struct or class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am working on OOPs and using C++. I have one class accessing object of other class/struct.
struct data
{
int a;
int b;
string str;
} sd;
class format
{
int x;
void show()
{
cout << data.a << endl;
}
};
which one is best to use here class or struct?
First of all, it's struct, not strut.
Second, you cannot access member a like you do, data.a, but rather sd.a, because you need to access it on an instance, not on the name of the struct.
For the detailed differences between class and struct see this SO question and its two best rated answers.
I use this convention:
A struct only have members that it make sense to manipulate directly
A class may have complicated rules for assigning members
This somewhat fits well with the default accessibility rules. But as said before in this thread, the choice depends on convention.
that depends on your requirement the only difference in struct and class is in struct all members are public by default and private in case of class

About delegates 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 9 years ago.
Improve this question
How do I specify the parameter of a method as any class that implements a specific interface ?
This is rather common in objective c.
There are no interfaces in standard C++, but we can simulate them pretty easily:
class IComparable
{
protected:
IComparable() {};
public:
virtual ~IComparable() = 0 {};
virtual int Compare(const IComparable& other) const = 0;
};
There is no way we can instantiate this class. It is effectively an interface. You can then derive concrete classes from this.
If you have an "interface" or abstract base class called Base, then a function which can accept any object implementing that interface would look like:
void fn(Base& obj) {
/*use Base functions on obj...*/
}