what is the differene between "class" and "struct"? [duplicate] - c++

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 2 years ago.
When writing C++ code, what is the difference between using class and struct?
class Foo final { };
vs.
struct Bar final { };
When should I prefer one over the other? Is this merely a choice of programming/coding style?

A struct simply defines the layout of a piece of storage.
A class is a dynamically-allocated "thing" which can have methods ... it can do things.

Related

Argument passing of template class as an interface [duplicate]

This question already has answers here:
Templates polymorphism
(7 answers)
Closed 1 year ago.
Here is my scenario:
class Container {};
class Papa {};
class Child: public Papa {};
void myFunction(Container<Papa> container);
Now I want to pass two different parameters to myFunction().
int main()
{
Container<Papa> papaContainer;
Container<Child> childContainer;
myFunction(papaContainer);
myFunction(childContainer); // this line is not working
}
apparently I cannot do this.
no suitable user-defined conversion from "Container<Child>" to "Container<Papa>"
What would be the solution for this case? How Container<Papa> can be used as interface here?
This seems similar to this question here: No Suitable User Defined Conversion
although that one is using numeric types. Is there anything in myFunction, or that myFunction returns that would need to differentiate Child from Papa?

C++ iterate through struct by increasin pointer bit by bit [duplicate]

This question already has answers here:
Iterate through Struct and Class Members [duplicate]
(6 answers)
Closed 5 years ago.
I am currently trying to iterate through a struct var in C++. I've got a struct var but I need to access specific elements of the struct.
someStruct {
int a;
int b;
bool c;
...
};
&someStructVar+1 would increase the memory by the size of the struct. But I need to increase the memory address by one bit after another. Is this possible? Is there any other approach?
What you're asking for is reflection, which C does not support. You can't get a list of fields of a struct and iterate through them.
You'll need to explicitly call out each field by name.
You can't "iterate" members of struct through pointer arithemetics, this is not allowed in C++.
The only thing you can iterate with pointer arithemtics are C-style arrays.

Deriving from an enum in C++ [duplicate]

This question already has answers here:
Base enum class inheritance
(13 answers)
Closed 5 years ago.
Is it possible to derive from an enum, if so how?
For instance:
enum eStandardTypes
{
Type1 = 0,
Type2,
Unknown,
Count,
};
enum eExtendedTypes : eStandardTypes
{
Type3 = eStandardTypes::Count,
Unknown,
Count,
};
No this is not possible, even with enum classes.
Support for inheritance of enum classes was discussed for C++17, but was not incorporated into that standard.

How to prevent your class from being inherited in c++? [duplicate]

This question already has answers here:
Prevent class inheritance in C++
(9 answers)
Closed 6 years ago.
How to prevent your class from being inherited in c++?
How to prevent your class from being inherited in c++?
Mark it as final.
class MyData final {
// ...
};

are default constructors for struct called just like class in C++ [duplicate]

This question already has answers here:
Struct Constructor in C++?
(16 answers)
Closed 9 years ago.
Do C++ struct also call constructors (default, copy constructors) and destructors just like classes or they follow the C language guidelines for struct? So in the example below, is a default constructor called?
Foo structObject; \\Foo is a struct
Yes, they do. The only difference between struct and class in C++ is in visibility of it's members. Struct has by-default members public, class private.
Effectively, writing
class A {
public:
//// ...
}
is the same as writing
struct A {
//// ...
}