C++ Struct definition explanation [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I read about defining a struct like:
struct someStruct {
int x;
int y;
};
struct otherStruct : public someStruct {};
So my question is about the definition of otherStruct.
What does this definition do?
I'm new in C++ so i only want to know under which key word i can find the definition of otherStruct to read about them in a book.

This answer is just a scratch on the surface of the inheritance concept of OOP and it does not cover all its aspects. You should read a book about C++ (or about OOP in general) to get a complete answer.
The part struct otherStruct : public someStruct says that otherStruct extends someStruct with public inheritance. In simple words, public inheritance does not change the visibility of the members (properties and methods) inherited from the base class.
The declaration block of the new struct ({}) is empty. It does not add any new members to those inherited from struct someStruct.
If you compare someStruct and otherStruct by their memory footprint and behaviour, they are identical. But they are different types and they cannot be replaced one for the other.
However, a pointer to a variable of type otherStruct can be used where a pointer to struct someStruct is expected (because otherStruct, by extending someStruct has all the properties expected from someStruct) but the other way around is not possible.

Related

C++ difference between : , :: operators [closed]

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
I new to programming. In stack overflow i couldn't see difference between : & :: is mentioned. Could anyone can explain in detail it helps to beginner learners like me. Thank you.
So you would use :: when you're defining/using methods from a class, so like for example
class foo{
public:
int bar;
int hi(int x);
int func(); // static member function
Foo(int num): bar(num) {}; // use of a colon, initialization list
};
int foo::hi(int x){
//define the function
}
Also if you have static member functions, you can just call those whenever through using foo::func(). You can find more about static member functions online.
The single colon is for member initialization list (you can look this topic up online) where you can initialization member variables in the construction of your class.
You can also find single colon used in polymorphism, when you derive a class from a base class. You can find more information about c++ polymorphism online.

Define enum in class or in File? [closed]

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 7 years ago.
Improve this question
Say that in file foo.h I have:
enum bar {
ONE = 1,
TWO
};
class foo {
bar m_bar;
public:
void setBar(bar arg){ m_bar = arg; }
bar getBar() const { return m_bar; }
};
In my current design, the only persistent bar variable will be m_bar. But I will have other functions, outside of foo that contain a bar, for example a GUI class that creates a local bar and passes it to setBar.
So here's my question, is there any rationale to defining bar publicly inside foo versus just inside the class where it is?
So here's my question, is there any rationale to defining bar inside foo versus just inside the class where it is?
If all the functions that create/work with bar are related to foo functionality, then it is perfectly acceptable to write it like this:
class foo
{
enum bar {
ONE = 1,
TWO
};
};
void process_bar_of_foo(foo::bar bar_value); // process the bar of a foo
If on the other hand you can write code that has (conceptually) nothing to do with a foo instance but deals with bar values, you should probably write it separately.
Well, as it stands, you can create bar objects with objects and functions outside of the foo class like you mentioned and then pass it to whatever foo object you make.
If you were to, however, define it in the class, then you wouldn't really be able to make bar objects unless you create a foo class first which can lead to unnecessary overhead if you just want to enum object.
And so it really depends on what you're going to do. If you only plan on using bar with the foo class, then it would be perfectly acceptable to define it there. Otherwise if it's going to be accessed elsewhere, leave it as is.

Deciphering C++ code: function in struct with same name as the struct [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to pick up a programming. I got this code from ai-junkie.com. I don't understand the last lines of each struct
SNeuron(int NumInputs),
SNeuronLayer(int NumNeurons, int NumInputsPerNeuron).
What is the purpose of this? Does anyone care to teach basic c++?
struct SNeuron {
//the number of inputs into the neuron
int m_NumInputs;
//the weights for each input
vector<double> m_vecWeight;
//ctor
SNeuron(int NumInputs);
};
struct SNeuronLayer {
//the number of neurons in this layer
int m_NumNeurons;
//the layer of neurons
vector<SNeuron> m_vecNeurons;
SNeuronLayer(int NumNeurons, int NumInputsPerNeuron);
};
It's a constructor. And someone has applied a pointless comment ctor to it. It allows you to instantiate an instance of SNeuron using code like
SNeuron sn(5);
This helps program stability. In C, you'd have to populate the structure fields yourself having instantiated an instance of the structure. That can leave a structure in an ill-defined state. In C++, the instance can be fully created in one step.
Remember that in C++, a struct is exactly the same as a class: with the exception that in a struct all member functions and member data are public by default (whereas in a class they are private by default).
This is a C-tor for the struct SNeuron that accepts an int.
in C++, a struct is simply a class with all members public by definition. And like any class it can have a c-tor, d-ctor and other C++ elements.

When to use struct or class? [closed]

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

C++ Reference Length Performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
My question is simple, what is the performance loss due to reference length. I cannot explain myself but here is the sample:
between this
C* pC = m_a->m_b->m_c;
and this expression
C* pC = m_b->m_c;
I am asking this because I have a global class which has a Singleton pattern and holds everything. I am accessing all of its members from its members like this.
class Global
{
A* a;
X* x;
};
class A { B* b; };
class B { C* c; }; // etc
class X { Y* y; };
class Y { Z* z; };
class Z
{
void foo() { Global::GetInstance()->a->b->c->foo(); }
}
Is this a good design? Any advice for this? I am having some trouble with this topic too Qt Architecture Advice Needed
Every -> operator is an indexed indirection, which costs a cycle or two, depending on the processor, and may be invisible if its pipeline is good enough.
However the real question here is 'compared to what?' What other implementation techniques are you considering for solving this problem? Unless you have a viable alternative your question is really meaningless.
Similarly the frequently-asked question about the relative efficiency of virtual and non-virtual functions is meaningless unless it takes into account how to get the same effect both ways. In the non-virtual case this amounts at least to an 'if' or 'switch', whose cost has to be added in to the comparison.