Class inheritance obsecure - c++

I was going through a old code written by someone i encountered one class defined as
class SomenameofClass::Someanothername of the class
{
//some code goes here
};
what does it mean ?
does it signifies private inheritance ?

This is the definition of a nested class which was declared elsewhere like this:
class SomenameofClass
{
class Someanothername;
};
Usually this is done when the nested class is only used in the implementation of the outer class, so its definition doesn't need to be exposed in a header file.

Nested classes are considered to be within the scope of the enclosing class and are available for use within that scope. To refer to a nested class from a scope other than its immediate enclosing scope, you must use a fully qualified name.

Its nested class, these are used to avoid name conflicts among different scopes. If a class is used by only one class, nest it, it will avoid naming conflicts and you will be notified if there is any conflict by intellisense

Related

How to let class have file scope? [duplicate]

So, I know static functions are functions that are local to the file. Thus, they can't be accessed from other files. Does this work for classes too? I've read a ton of controversy on how static class does not declare the class to contain purely static members and methods (which is obvious), but couldn't find anything that mentioned whether or not this would declare the class to be locally accessible to the file scope, as is more logical.
In case it doesn't, what about using an anonymous namespace, which I've heard also can be used to declare file local functions?
You can define a class in unnamed namespace as for example
namespace
{
struct A {};
}
In this case the class name will have internal linkage. That is it is visible only in the compilation unit where it is defined and all compilation units that include that definition will have their own class definitions.
As for the storage class specifier static then (7.1.1 Storage class specifiers)
5 The static specifier can be applied only to names of variables and
functions and to anonymous unions
Does this work for classes too?
No. no such 'static' keyword for class.
As an alternative to an 'anonymous namespace', you can declare a class (Foo) and its definition (implementation) entirely within a single cpp file. The only code which can use this class is the code below that declaration ...
File X.cpp:
// Foo declared
class Foo
{
public:
//...ctor
//...dtor
// etc.
}
// ... Foo defined (implemented)
Foo::etc() { ... }
// implementation of X - only X has access to Foo.
End of X.cpp.
And File X.hpp does not reference Foo.
If you subsequently discover a name collision (i.e. linker reports duplicate symbol), your only choice is to change one or the other name.
There are many Q&A's in SO about anonymous namespaces. I am careful what kinds of things I put into one, and agree they can prevent name collision.
I have used both techniques.

Clarification on classes and scopes in this scenario

I'm currently working with the JUCE Framework to create an audio VST plugin to get to grips and learn, but just want to clarify some basic stuff relating to classes.
in my header file, i have a class EQPLUGProcessor and inside that class i call static juce::AudioProcessorValueTreeState::ParameterLayout createParameterLayout();
When i call the function createParameterLayout() in my .cpp i have to write juce::AudioProcessorValueTreeState::ParameterLayout EQPLUGAudioProcessor::createParameterLayout(){}
My question is, why do i have to include the juce::AudioProcessorValueTreeState::ParameterLayout before the actual scope that the function is in ( EQPLUGAudioProcessor)? Surely i should be telling the compiler to look in the EQPLUGAudioProcessor and thats it?
I get that EQPLUGAudioProcessor is the class which its all inside, but still cant seem to understand when, where and why i'd need to clarify the classes that the function comes from again in the .cpp?
Let me know if this requires clarification.
An enclosing namespace or a class does not have to be specified only inside the same namespace or a class:
class store {
class give_me {
// ...
};
static give_me something_cool();
};
Here, the declaration of something_cool() only needs to reference give_me, rather than store::give_me. This is because this declaration appears inside the declaration of its class.
Now that this class is declared, and it's time to define it's class method, everything must be spelled out:
store::give_me store::something_cool()
{
// ...
}
If the class method returned a void instead you'l still have to write something like:
void store::something_cool()
{
// ...
}
You already understand that you can't just write void something_cool() and define this class method. This would only define some unrelated function with this name.
You have to write store::something_cool because this definition no longer appears within the store scope.
Well, the same thing applies not just to class methods but also to inner classes (and also other kinds of symbols that are declared in some enclosing scope). Since give_me is not a class that's declared in global scope, it is an inner class, when in global scope you must reference it as store::give_me.
That's just how C++ works. There are also various complicated rules that define where a scope begins, and ends, with respect to C++'s syntax, that's tangentially related to this. In some cases it is possible to take advantage of these scoping rules and avoding explicit scope references by using an auto declaration with a trailing return type; but how to do that will have to be a different question for some other time.

How does static worked with all class objects using ClassA::m_variable

We recently found issue with our project caused by this error:
namespace sim
{
class ClassA
{
private:
static std::list<uint16_t> m_variable;
}
std::list<uint16_t> ClassA::m_variable;
}
So what happened is that m_variable became static for all instances of ClassA, not just for the particular instance of it. I'm not an expert with C++ so please bear with me if the problem is staring directly at my face, I'm still on the learning phase.
Can someone explain why this happened?
After your comment, you want to understand how to use static members of class.
For normal data members, one instance of the member exists in each and every instance of the class. OTOH a static data member exists only once and is shared among all object instances of the class.
Simply the simple declaration of a static member inside a class definition is only a declaration. Because of the One Definition Rule, that object also needs a definition.
In C++ language, that definition normally occurs outside of the class definition, so the line std::list<uint16_t> ClassA::m_variable; is required.
There are exception to that rule, mainly for static const integral members that can be defined inside the class definition provided you only use their value and not their address (search for One Definition Rule if you want to go further that way). But this is a rather advanced corner case, so if you are a beginner just remember that a static member of a class need a definition outside the class definition.

why to use classes inside namespace

I wish to know the actual reason behind writing the class names inside namespace. What purpose do they solve? Do they include the classes or do something else? Any help is greatly appreciated.
namespace ns3 {
class Channel;
class SpectrumChannel;
class MyMod; ;
class NewModule : public NetDevice
{
public:
// methods and data
}
It is good practice to put your classes into a namespace in order to avoid a name collision with other code (libraries) you might use (also coming in later in a project). When using common names like Vector, Logger, etc. as class names it can easily happen that they are also used in other code you want to use. When you put your classes into a (well considered) namespace, this chance of a name-collision is minimized.
Forward declarations are a different (independent) topic. When classes are defined within a namespace, also their forward declarations must be done within that namespace.

Unnamed namespaces vs private variables

I have been reading through other questions on here and there is something that has me confused and hopefully it can be explained. I am sure there it is a simple thing but it is alluding me.
So in C++ we have private variables that are only viewable within the class:
class MyClass
{
private:
int i;
};
But we can also have unnamed namespaces:
namespace
{
int i;
}
Both appear to be private to the class but in the 2nd case you cannot see they exist from the header file. From reading other questions it seems that functions are different as you can't pass class objects to them? But I am not sure what the difference is here for variables.
Is there a disadvantage to the 2nd way that means you should still use private variables?
They aren't the same.
Integer i in the anonymous namespace will be shared by all instances of MyClass.
The private integer i in MyClass will be unique for each instantiation of the class.
The equivalent using private would be to make i static:
//.h
class MyClass
{
private:
static int i;
};
And instantiate the one single shared i like this:
//.cpp
int MyClass::i = 0;
Both appear to be private to the class ...
No, only the first is private to the class. It's a non-static member variable; one is instantiated in every object of the class type.
The second is not in a class at all; it has static storage duration, so one is instantiated for the whole program. Anything that accesses it is accessing the same variable as anything else that accesses it. Being in an unnamed namespace, it's only accessible within the translation unit (i.e. the source file) that defines it; but it's accessible to any code there, not just a particular class.
Is there a disadvantage to the 2nd way that means you should still use private variables?
If you want a copy of the variable in each class object, then you need it to be a non-static member.
If you want to share it between all objects, then it's up to you whether to make it a static member, or put it in a namespace inside the class's implementation file. I often do the latter to simplify the class definition. Disadvantages are that access isn't restricted just to the class but to anything else in that file, and you can't access it from any code that you might want to put in the header.
Namespaces are unrelated to objects/classes. In particular, if you have two objects, each has its own copy of a private variable.
They are quite different concepts. The private data member is visible only to a class, and in the non-static case, each class instance owns one of these. The anonymous namespace allows you to make code available only to other code in the same file. So in the case of the single int variable, all code defined in the same place as the anonymous namespace would see the same, single variable.