This question already has answers here:
Can a c++ class include itself as an member?
(4 answers)
Closed 5 years ago.
I'm trying to define a class this way (simplified):
class Student
{
private:
Student* pointer;
public:
set_pointer(Student*);
}
Is this legal in C++ and is it consistent with object oriented design?
Yes, a class may store a pointer to an object of the same type (which may be itself, or some other instance).
This is common in linked-list implementations, where a node stores a pointer to the next node.
As for whether it's "consistent with object oriented design", it is impossible to make a generalisation on the subject; it depends on the program. Personally I cast a suspicious eye on code of this form, but again you can't really generalise.
Related
This question already has answers here:
What are forward declarations in C++?
(8 answers)
Closed 2 years ago.
I'm trying to implement something along the lines of:
Class EntityManager
{
AddEntity(Entity e);
RemoveEntity(Entity e);
std::vector<Entity> entities;
}
Class Entity
{
// Need a mechanism here to add and remove entities on the constructor and destructor of Entity
}
Hope this makes sense. Eventually Entity will contain a number of pure virtuals, when I inherit from Entity I want it to automatically add the entity to the vector, the EntityManager will then call the pure virtual functions on each entity.
Thanks for your time.
If you're using virtual functions you need pointers, so Entity* is your base type here, or perhaps one wrapped in a smart pointer.
When you use Entity you're saying "these elements are all literally Entity and are exactly the same size" while Entity-derived classes may be quite different and significantly larger.
As a pointer there's no size information baked in, that's all a function of how it was allocated, which is typically via new with the correct class called.
If you want to automatically add these to something as they're created then you need to add code to your base class constructor.
So long as the adding code is in the .cpp implementation of Entity then there's no circular references problem. Declare Entity first and the EntityManager knows enough to build on.
This question already has answers here:
Why do Python classes inherit object?
(6 answers)
Closed 6 years ago.
I am learning to make classes using template codes and one of them is coded with what looks like inheritance syntax:
class KNNLearner(object):
What is the purpose of (object) here?
In python everything is an object, an object is an object. You can also create Meta-classes to change some behavior of your objects/object to implement something.
In you case KNNLearner(object) the (object) permits you to pass the class that you want to KNNLearner to inherit.
This question already has answers here:
How can I add reflection to a C++ application?
(28 answers)
Closed 7 years ago.
Is there a way that I could obtain a list or maybe iterate through a list of each member variable of a class -regardless of it's type.
My intention is to add them all to a template std::map.
class Person
{
int Age;
char *Name;
template<typename Container> std::map<char *, Container> List;
void MakeList()
{
for(auto i : Person.memberVariables)
{
List.emplace(i);
}
}
};
Could I do something like the above to add each variable of my class to the list, regardless of how incorrect the other code is. The adding to the list is what I'm interested in.
Thanks in advance.
No, you can't do it in C++. this is called reflection which is currently not supported in C++.
The ISO C++ comitee talks about compile time reflection that is proposed for C++17 that will let you to iterate over class members on compile time. but it will take much time until this feature will be standarized and be implemented on the common compilers
as a side note, I don't see why one would want to iterate over member variables other then maybe implementing serialization method , which in this case I would require the class to implement serialize method and enforce it with SFINAE.
If there is, I would be very surprised.
This is called reflection, and as far as I know this isn't supported by C++.
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 years ago.
I know that is possible for a struct:
struct A{
A* p;
};
But could we do that to a class:
class B{
B*p;
}
I tried to google it, but end not found it. Thank you very much!
Yes. There is no difference between a class declared with struct and one declared with class other than the default access for members and bases.
Yes, a class can contain pointers to a class of the same type, and the symbol representing the class can appear in signatures of functions or in the bodies of functions defined in that class. Furthermore, it should be noted that, in C++, struct and class are identical with only one difference -- the default access level for a struct is "public", while the default access level for a class is "private" (of course, one can trivially write "public:" or "private:" at the beginning of either in order to change the access level; it is only convention that results in "class" being used for types that include more functionality, encapsulation and "struct" for pure data objects).
This question already has answers here:
c++ sizeof() of a class with functions
(7 answers)
How do you determine the size of an object in C++?
(11 answers)
Closed 8 years ago.
Suppose:
Class A{
float one;
float two;
float three;
//... many, many member functions, and maybe static functions too ..
}
Can I assume that, no matter how many functions are in this class, the following should generally be true:
sizeof(A)==sizeof(float)*3
Such that I could even assert this:
static_assert(sizeof(A) == sizeof(float) * 3, "Your class is padding memory.");
Is this accurate?
Now, suppose class A inherits from another class. I assume the above would not be true, and rather you'd have to include in the sizeof assertion the addition of the size of ivars from the base class?
if "Class A" has virtual functions or derives from class(es) having virtual methods then
the sizeof(A) cannot be accurately determined by manually summing up the data members.
This is because compiler inserts a hidden pointer called as v-pointer which points to v-table.
Things get more complicated if you have virtual base class, since a hidden v-base-pointer might get inserted by the compiler.
Also, the position where these hidden pointers get inserted within the memory model of a polymorphic C++ object changes across compiler implementation (since these specifics are not part of the C++ standard).
This is one such reason why we can't achieve binary interface compatibility (Compile once and reuse across all platforms) of applications developed using C++.
(Again, this is another reason why .Net and Java was invented!).
In short, the memory model of a polymorphic class in "C++" doesn't match with that of "C" (that's one reason why we have extern "C" in C++).
many, many member functions, and maybe static functions too
If you class A contains virtual functions, then
sizeof(A)==sizeof(float)*3
will not be true since there is extra memory for vtable/vptr.