About class's(OOP): i need some information. And Multidimensional Vectors

Pages: 12
we have 1 class variable. like we know the class have methods... and some of that methods can be another class variable(like nested class's).
my question, no code just asking, how can i create a nested method list and know where i'm(thinking on dot)?

sample:

1
2
car renault;
renault.engine.oil=100

(the engine is another class that have oil method... we are on engine class)
Last edited on
To access the nested class you need the scope operator ::

In your example engine is a member variable that can have the type of the nested class. oil is a member variable as well. Like so:

1
2
3
4
5
6
7
8
9
10
11
class car
{
public:
  class engine_type // It is wise not to name the class like the member variable
  {
  public:
    int oil=0;
  };

  engine_type engine;
};
Now you can use it like shown in your sample.
i'm sorry, before continue i need ask: is my topic\question confused?
yes
coder777: now i see what you mean. thank you.
now i need more advices(thinking on text).
i can get the 1st class name 'car' and it's members\methods names. but some methods are nested, like you show on that sample.
i need maybe using a vector, i can save the class name, it's methods, the other class name and it's methods, the save all.. to be honest i don't know use a multidimensional vectors.
and the number of dots can tell me the vector level.
can you advice more about these?
i need a vector with all 'car' members and the 'engine_type' members(multidimensional vectors).
i can do these:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vector<string> ClassMembers;

 class engine_type
  {
  public:
    int oil=0;
  };

class car
{
public:
 int b;
void hello
{
    cout << "hello";
}
  engine_type engine;
};

// don't think how i get the words... just how i can add them on vector:
ClassMembers.push_back("b");
ClassMembers.push_back("hello");
ClassMembers.push_back("engine");

now see these: the 'engine' have the 'oil'.
how can i add it on vector?
i'm sorry only now i get it better, but sometimes isn't easy when the things are new to me.
after these i will need more 1 advice about the dots. but now maybe you can get the point of what i need ;)
Wait... are you trying to dynamically change the definition of car by adding new data members, while the program is running?

You can't do that in C++, like you can in, say, Python.

What you could do is have your class store a dictionary (read std::map) of names attributes. The key would be a string, storing the name of the attribute. The value would be the value that you want the attribute to have.

However, you're going to have to deal with the fact that C++ is strongly typed. The definition of your attribute map will have to define what type value is, which will restrict what values you can have.

There are ways around this. If you're using C++17, you can use std::any. If not, you can get the Boost libraries and use boost::any. Or you can do something really ugly and error-prone, and use C-style void pointers.

But what I have to wonder is: why are you wanting to do this? What problem are you trying to solve, for which you think you need to do this?
Last edited on
i's learning what i can do... then i can add it on my code.
thank you so much for all.. thank you
So... you're going to completely ignore the questions I've asked? After I took the time to help you out?
MikeyBoy: i'm so sorry.
"Wait... are you trying to dynamically change the definition of car by adding new data members, while the program is running?"
not that.. i'm trying getting(think on text and not code) the class members names.

"What you could do is have your class store a dictionary (read std::map) of names attributes. The key would be a string, storing the name of the attribute. The value would be the value that you want the attribute to have."
on map can i have a name and the group members, right?
if so, i can use an array for all classes.

"However, you're going to have to deal with the fact that C++ is strongly typed. The definition of your attribute map will have to define what type value is, which will restrict what values you can have.

There are ways around this. If you're using C++17, you can use std::any. If not, you can get the Boost libraries and use boost::any. Or you can do something really ugly and error-prone, and use C-style void pointers."
yes the C++ have several types... but i don't wan't the Boost libraries or i will get more dependencies... and i don't wan't that.


"But what I have to wonder is: why are you wanting to do this? What problem are you trying to solve, for which you think you need to do this?"
i'm learning what i can do... then i can add it on my code.
without know it, i can't use.
i was trying answer you because of map... but i'm sorry something

There is no straightforward way to introspect types for member objects or member names. In many cases it's possible to ask for this information, but the programmer needs to build up the information themselves, for example, in traits classes, or perhaps by using a macro.

If all you need is the name of the members, go ahead and type the names of the members out in an array or vector and consider the problem solved.
1
2
3
4
5
6
struct A {
    int foo;
    int bar;
};

constexpr std::array member_names_for_A{ "foo"sv, "bar"sv };
http://coliru.stacked-crooked.com/a/fea72db5928f5adc

Otherwise, one can use traits classes to access the member objects of a structure like one would iterate a std::tuple (with the well-known indices trick), find their names and sizes and things and access them.

See the discussion here:
http://www.cplusplus.com/forum/general/229608/

I'm learning what i can do... then i can add it on my code.

It's much easier to answer questions with a goal.
Last edited on
not that.. i'm trying getting(think on text and not code) the class members names.

OK, but to what purpose? What will you achieve by doing this?

If all you want to do is output a list of the members of the class, then, yes, you can store those names as a vector. Or, hell, as a single string. Why bother with a container at all? Do you intend to manipulate those names in any way? Iterate over them? Enumerate them?

This feels like an XY problem to me, although I'm not sure you even have an X at all in the first place.

on map can i have a name and the group members, right?
if so, i can use an array for all classes.

A map is an associative container. Each entry in the map has a key - which can be any type - and a value - which can be any type.

On a very simplistic level, you can think of it as being a bit like an array, but instead of the indices being consecutive integers starting at 0, the indices can be any type. So, if your map has keys of type std::string, the elements of that map can have keys like "b", "hello", "engine", and you could access the values of the elements using my_map["b"], my_map["engine"], etc.

But it sounds as though you don't actually want to associate any values with these names; you just want the names themselves. In which case, you don't want to use a map.

yes the C++ have several types... but i don't wan't the Boost libraries or i will get more dependencies... and i don't wan't that.

Any particular reason? Using 3rd-party libraries is a big part of C++ coding, once you move on from simple tutorials. Admittedly, Boost can be intimidating.

i'm learning what i can do... then i can add it on my code.
without know it, i can't use.

But why? Why do you want to use this? What is the problem you are trying to solve by having your class store a list of the names of its members? What do you want to achieve, that you think this will help with?

"OK, but to what purpose? What will you achieve by doing this?"
is for i use and add the class on my compiler.
if i use class object and class member, i must test if returns a value or if it's a member ;)
@Cambalinho

Hi,

In addition to the existing answers:

This is classic XY problem:

Cambalinho wrote:
if i use class object and class member, i must test if returns a value or if it's a member ;)


You have come up with a possible solution Y, but we don't know what the problem X is. We might have a better solution.

Please provide a detailed example of what you are doing exactly. :+)

Maybe this can be solved with virtual polymorphism, or maybe it requires some Template Meta Programming (TMP), a design pattern (DP), Policy Design, a mixture of those, or something else : we can't say.

As an example, say we have various Transport Mechanisms: walking, riding a bicycle, moped (electric or petrol), motorbike, car (petrol, diesel, electric, LPG, solar), ride an animal (donkey, horse, camel). We want to know what resources are needed to use a Transport Mechanism, noting animals including humans need food and water. We want to add new things without having to change the code too much. Using virtual polymorphism, it could look like this:

1
2
3
4
5
6
7
8
9
10
11
12
class TransportMechanism;

class Vehicle : public TransportMechanism;
class Animal  : public TransportMechanism;

class Resource;
class Fuel : public Resource;
class ElectricCharging : public Resource;
class SolarEnergy : public Resource;

class Food : public Resource;
class Water : public Resource;


There could be more derived classes, or use a Factory DP to make them. The resources may not be limited to things like fuel, one could have Sat Navs and other equipment, licensed drivers, approval to use etc.

The base classes would have pure virtual functions that are common, this is the interface.

Provide containers and functions that are written in terms of the base classes. For example a concrete object like MyVeyron could inherit from the TransportMechanism base class a: std::vector<std::unique_ptr<Resource>> RequiredResources

This vector would have concrete resources like petrol or food say push_back or emplace_back into it, in the derived class object. Polymorphism means that the derived class can then execute the correct function.

The Resource base class could have a pure virtual function or ostream operator that prints out what a particular resource is. The concrete resource class would have a member variable (also inherited from the Resource base class) whose value contains the actual data to be output by the ostream operator.

Naming of classes and deciding on what classes are inherited is important. For example, say we want to make the distinction between a bobsled, a horse drawn carriage and the other vehicles. We can make new classes and name them properly to accommodate these requirements, sometimes this means making new intermediate classes or even creating new base classes.

Going back to the car with engine example, we might have two-stroke and four stroke engines: Two Stroke has fuel with oil already in it; Four Stroke has fuel and separate oil. It is possible to implement this with virtual polymorphism.

Some other more advanced ideas:

https://en.cppreference.com/w/cpp/types#Type_traits_.28since_C.2B.2B11.29
https://en.cppreference.com/w/cpp/concepts
https://en.cppreference.com/w/cpp/experimental/constraints

There are plenty of members here who know all about polymorphism, if that is a solution to this problem, but first we need to know what the problem is. :+)

Good Luck !!






i'm trying to tell you all what i'm doing, but everyone go to class code. and i said class text and not code ;)

imagine that these class members(like i said only text, but i must show the members):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class engine_type
  {
  public:
    int oil=0;
  };

class car
{
public:
 int b;
void hello
{
    cout << "hello";
}
  engine_type engine;
};

car Renault;

//....
Renault.engine.oil=100;

now forget the code...
the car have some members, so i must do a list class members... but the engine have more members.
i must test if 'engine' it's a Renault member.. and 'oil' it's an 'engine' member.
reading that class word by word, i can get it's members on a string.
but the 'engine' have more members.
my objective is testing if the 'engine' it's a Renault member.. and 'oil' it's an 'engine' member. for that i must have a string array\vector.. but the engine it's better use another array, right?
1 array by a class.
i was trying getting more ideias on how i can save the class members even with nested class's.
i was trying explain... but i'm sorry about my English... i'm sorry
and i said class text and not code ;)


By class text, I think you mean pseudo code :+)


... but i'm sorry about my English...


Could you use Google Translate ? Type in exactly what you want in your language, convert to English, then post it here. I gather it's not perfect , so post the text in your language also. There may be someone here who is fluent in that language, they can sort out any inconsistencies with grammar.

https://translate.google.com/

You still seem to be doing the XY problem thing, you are explaining individual steps for what you are are doing to solve the problem, but we still don't know what the actual broad problem is. However the following might be what we were after :

Cambalinho wrote:
reading that class word by word, i can get it's members on a string.
Cambalinho wrote:
i was trying getting more ideias on how i can save the class members even with nested class's.


Ah, I wonder if you want to save all your data to a file, then another time read the data from the file to recreate the objects? If so, this is called serialization.

If this is not what you want to do, then you need to explain as generally and basically as possible what the goal is. Don't describe any data structures, or any steps involved. We want "Make a Cake", not the recipe.

By the way, I wouldn't have:

car Renault;

Instead the car class could have a member variable std::string Make; . Then you will probably have a container of all the cars. When you emplace_back the constructor will assign "Renault" to the Make variable.
imagine these class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class engine_type
  {
  public:
    int oil=0;
  };

class car
{
public:
 int b;
void hello
{
    cout << "hello";
}
  engine_type engine;
};

car Renault;

//....
Renault.engine.oil=100;

been saved on text file.
i read the entire class word by word and line by line.
using a vector i can add the class members.... the 'engine' is best on another vector(multidimensional vector's).
what i was trying to get more ideias was for test these line:
Renault.engine.oil=100;
if the 'engine' it's a 'Renault' member.... and if the 'oil' is from 'engine' member.
but after several reading i come up with the best idea:
- using a 2D multidimensional vector, i can save the class name and it's members names;
- if the class member it's from another class, it's the best save the class name on vector 1st level.
yes now i'm on right way for resolve the problem.
and for use a 2D vector i must use it in these way:
VectorName[index].push_back(value);
it's easy. but if i'm wrong, please correct me ;)
i need another advice: it's good use the same vector for standard class's and user class's names?
Hi,

Even though you have almost repeated everything you have said earlier, it does kind of sound like you want serialization, but that is still a guess on my part. Instead it could be you want a code generator, or just a simple ostream operator. You have again listed steps for solving the problem and specified a data structure - the vectors. Let me reiterate this, we want a description of the problem like: Make a Cake, not the recipe for the making the cake

i read the entire class word by word and line by line.
...
but after several reading i come up with the best idea:
...
yes now i'm on right way for resolve the problem.
...
it's easy. but if i'm wrong, please correct me ;)


You seem to have already decided on the best way for solving the problem, but seem to not realize that there may be a vastly better way than your solution, one you haven't heard of before, if only you would tell us what the general problem is. We can't for sure correct your problem, because we really aren't sure what the problem is yet. This why the XY problem is so bad.

Another way of putting it is: What do you want to do with this info in the vectors? Why is it important to have this data? What is the motivation for writing this code?

Here is another example of a XY problem:

In an underdeveloped country, there is 10 tonne of firewood which needs to be moved 1 kilometre. The local chief is adamant that the only way to do it is to have all the 100 village people move it by carrying it in their hands, taking the whole day. The person from the United Nations offers the use of a truck which can carry all of it in 1 load. From the chief's point of view, this is amazing: they had never seen a truck before :+)

If you give us the right info we can help you better :+)


hey don't shoot me :P
it's for i add on my compiler using the class's
Hi,

Look I am not trying to take you down, instead I have been trying for the last 3 days in various ways to get the right information from you, in order to help you.

Cambalinho wrote:
it's for i add on my compiler using the class's


You see, that 10 word sentence fragment doesn't help us at all. Could you type a 1 paragraph explanation into Google Translate?
TheIdeasMan: now i'm feeling dummy :(
honestly maybe i'm learning something with your attention ;)

i'm creating a compiler.. my compiler just convert my own language(it's the combination of VB and C++: been powerfull and easy to use) to C++, and then i call the C++ compiler, free, for i create the exe file.
my language must have class's too.
for i use class's instances, i must test if the class was created and if the class member was created.
so for that i must, when the class is created(after testing all errors), create a vector for all class's names and it's members.
some class's can have variables from another class's... but i think, the best is create just 1 vector for all class's names and it's members(if the member it's from another class... i just must know that it's a member.
and i have notice that i must create a structure for class name and it's members and more information that i can use it.
and yes maybe i have the solution.
but how i found the solution!?! i found the solution doing these topic e and learn more from it.
thank you so much for all to all
Pages: 12