class myClass* holding; VS myClass* holding; [duplicate] - c++

This question already has answers here:
What is the difference between "struct" and lack of "struct" word before member of a struct
(2 answers)
Closed 8 years ago.
I faced this in a code I was reading:
class myClass* holding;
Is this means the same as this below:
myClass* holding;
Thanks,
EK

If at that point myClass is known as a valid class name (i.e. it was declared before) then yes, both declarations are equivalent. It should be noted though that the class name can be hidden by, say, a variable name, in which case using the elaborated version class myClass will allow you to work around the hiding and make sure that you refer to the class name specifically, as in the following example
class C {};
int main() {
int C;
C a; // <- invalid
class C b; // <- OK
}
If at that point name myClass is not known, then the second version simply won't compile. The first version will compile and will introduce a forward declaration of class myClass (see LihO's answer)

class myClass* holding;
is rather equivalent to:
class myClass; // letting compiler know that there is type myClass
myClass* holding;
unless you are using class keyword to resolve the ambiguity of myClass (see the other answer).

Related

In C++, whats the difference between initializing an object with (), {}, or neither of those? [duplicate]

This question already has answers here:
Why can't member initializers use parentheses?
(2 answers)
How to create an object in a form like this: ifstream in();
(1 answer)
What are the advantages of list initialization (using curly braces)?
(5 answers)
Closed 5 months ago.
In a class, if I have:
private:
MyClass myObj;
vs
private:
MyClass myObj();
private:
MyClass myObj{};
And assuming MyClass takes no parameter in its constructor.
MyClass myObj;
This declares a class member named myObj, that gets default-constructed, by default.
MyClass myObj();
This declares a class method, a class function, named myObj that takes no parameters and returns a MyClass object.
MyClass myObj{};
This also declares a class member named myObj, that gets default-constructed, by default, just like without the {}.
Welcome to C++.

What's the difference between MyClass myObject; and MyClass myObject();? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 4 years ago.
This code doesn't behave how I expect it to.
#include<iostream>
using namespace std;
class Class
{
Class()
{
cout<<"default constructor called";
}
~Class()
{
cout<<"destrutor called";
}
};
int main()
{
Class object();
}
I expected the output 'default constructor called', but I did not see anything as the output. What is the problem?
Nope. Your line Class object(); Declared a function. What you want to write is Class object;
Try it out.
You may also be interested in the most vexing parse (as others have noted). A great example is in Effective STL Item 6 on page 33. (In 12th printing, September 2009.) Specifically the example at the top of page 35 is what you did, and it explains why the parser handles it as a function declaration.
No call to constructor
Because the constructor never gets called actually.
Class object(); is interpreted as the declaration of a function object taking no argument and returning an object of Class [by value]
Try Class object;
EDIT:
As Mike noticed this is not exactly the same code as what you are feeding to the compiler. Is the constructor/destructor public or is Class a struct?
However google for C++ most vexing parse.
You can use it like this:
Class obj;
//or
Class *obj = new Class(/*constructor arguments*/);

Typedef With A Class In Another Class [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 5 years ago.
Improve this question
class A{
public:
A(){ letter = 66; }
void display(void){ cout << "A = " << letter; }
private:
int letter;
};
class B{
public:
typedef A classA;
};
int main(void){
B::classA objA;
objA.display();
return 0;
}
This code runs properly ;however, I cannot understand what we do in this code.
I have never seen something like this before. It seems like class A is a member of class B. If it is so, why do not we use classA through object of class B ?
It seems like class A is a member of class B. If it is so, why do not we use classA through object of class B ?
No. What you have there is a name (a type in this case) declared in class B as classA which stands as a typedef to a class, A. In C++, the name of a class is also a namespace. so the typedef, lets you access a name classA within class B.
I have never seen something like this before.
For your specific example, it may be an overkill. But having type aliases is very useful and you will find it a lot in many class templates codes.
Consider:
template<typename T>
class B{
public:
typedef T classA;
};
Without the typedef above, it would be very difficult to tell what type the above class was instantiated with.
A typical example is how STL algorithms can tell the category of a given iterator by accessing the name iterator_category from the iterator's type via std::iterator_traits
The class definition class B { typedef A classA; } does not define any data member in class B; with typedef, it introduces a new name classA as a synonym for type class A). Because this typedef-name is defined within class B, it is in the namespace of class B, such that you need to refer to this new type as B::classA, which is then equivalent to A. Confer, for example, the C++ standard:
7.1.3 The typedef specifier
(1) Declarations containing the decl-specifier typedef declare identifiers that can be used later for naming. ...
A typedef-name is thus a synonym for another type.
...
BTW: A common way for introducing aliases is also the using-statement:
using classA = A;
(2) A typedef-name can also be introduced by an alias-declaration. The
identifier following the using keyword becomes a typedef-name and the
optional attribute-specifier-seq following the identifier appertains
to that typedef-name. It has the same semantics as if it were
introduced by the typedef specifier. In particular, it does not define
a new type and it shall not appear in the type-id.

"class" Keyword on Return Type - C++ [duplicate]

This question already has answers here:
what is 'class' in 'class DataType* Variable' in Unreal Engine Shooter Game Sample
(3 answers)
Closed 7 years ago.
I came across a piece of code that looked like this:
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
What does the "class" keyword do on the return type? I can't find anywhere that explains what it's function is. Does it just specify that it's talking about SomeClass as a class in case there is some sort of ambiguousness or something? I am confused.
class SomeClass is a longhand way of referring to the class type SomeClass (technically, it's the elaborated type specifier). Usually, adding class is redundant, and the two are equivalent. But it's sometimes necessary to resolve the ambiguity, if there's a variable or function with the same name.
It is used to disambiguate.
Say for example if you have a variable of the same name in the same (or outer) scope, something like this:
int SomeClass; //SomeClass is declared to be variable here
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
Without the class keyword, the function declaration wouldn't make sense to the compiler. The class keyword tells the compiler to ignore the variable declaration, and look for a class declaration.
It's a forward declaration. It allows you to just say "there is a class SomeClass somewhere in my program, it is just not visible to this file in order to prevent redeclerations".
Whenever you implement this function, though, the file must have actual interface of class SomeClass.

does not name a type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Resolve circular dependencies in c++
What is forward declaration in c++?
I have two classes A and B. I need to have a field in each which is a pointer to an object of the other class. I get "does not name a type" since the definition of the class is yet to appear. For example:
class A{
B* b;
}
class B{
A* a;
}
gets me "B "does not name a type" at the second line.
Use forward declarations:
class B;
class A {
B* b;
};
class B {
A* a;
};
This way you're telling the compiler that B is going to be declared later on and that it should not worry. See this for more info: Forward declaration
Forward declarations is key to you question , here is the link
What is forward declaration in c++?