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;
Related
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();
}
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.
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.
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
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...*/
}