2

To create my own queue class, I need to declare a class attribute inside a function of this class.

What I am trying to do is :

when I call the function add, the program creates a new attribute to the class, in private, to access after with the operator []. By this I can create a queue without type restriction, using items of any class.

Is it possible?

Edit

This queue is just a use sample. My really question is: in python i can do this:

class Car:
    def __init__ (self, ...): 
        ...
        self.color = 'blue'

Now, color can be used in anywhere of class (remember that don't need insert type to declare a variable in python. In the exemple, color was been declared in __init__ scope, but is a class atribute, and not a local variable). How I can do something like this in c++?

Felipe Nascimento
  • 207
  • 1
  • 4
  • 10

2 Answers2

0

The question is not clear in the first place. From what I understand,you want to create a queue,with heterogeneous elements.You can refer this answer for heterogeneous containers.

Heterogeneous containers in C++ You can also use tuple to hold heterogenous data.

0

The concept you're looking for is a member variable, which must be part of the class definition because C++ is not dynamically typed. To make it only visible within the class, make it private. You can then set its value in the constructor initializer list.

class Car {
public:
  Car : color("blue") { }
private:
  std::string color;
};
Rakurai
  • 974
  • 7
  • 15