Difference between struct spell * spells; and spell * spells; [duplicate] - c++

This question already has answers here:
Is it optional to use struct keyword before declaring a structure object?
(3 answers)
Closed 10 months ago.
I noticed that you can declare a pointer to a struct in two ways:
struct spell * spells;
and
spell * spells;
what is the difference?

In C, struct keyword must be used for declaring structure variables, but it is optional in C++.
For example, consider the following examples:
struct Foo
{
int data;
Foo* temp; // Error in C, struct must be there. Works in C++
};
int main()
{
Foo a; // Error in C, struct must be there. Works in C++
return 0;
}
Example 2
struct Foo
{
int data;
struct Foo* temp; // Works in both C and C++
};
int main()
{
struct Foo a; // Works in both C and C++
return 0;
}
In the above examples, temp is a data member that is a pointer to non-const Foo.

Related

Syntax for pointers to a structure

When declaring a pointer to a struct, both the following code snippets compile without error:
A)
struct Foo
{
int data;
Foo* temp; // line in question
}
B)
struct Foo
{
int data;
struct Foo* temp; //line in question
}
What is the significance to repeating the word "struct" in the declaration of the struct pointer (as in (B))? Are there any differences compared to not doing so (as in (A))?
Thank you for your consideration.
In C, struct keyword must be used for declaring structure variables, but it is optional in C++.
For example, consider the following examples:
struct Foo
{
int data;
Foo* temp; // Error in C, struct must be there. Works in C++
};
int main()
{
Foo a; // Error in C, struct must be there. Works in C++
return 0;
}
Example 2
struct Foo
{
int data;
struct Foo* temp; // Works in both C and C++
};
int main()
{
struct Foo a; // Works in both C and C++
return 0;
}
In the above examples, temp is a data member that is a pointer to non-const Foo.

Why can't I assign const int in struct constructor? [duplicate]

This question already has an answer here:
Unable to initialize private const member [duplicate]
(1 answer)
Closed 4 years ago.
Why can't I do this in C++?
struct SomeStruct
{
public:
SomeStruct(const int someInt)
{
m_someInt = someInt;
}
private:
const int m_someInt;
};
Should the private field just be a regular integer?
You're assigning someInt to m_someInt, which is illegal. But initialization is okay.
struct SomeStruct
{
public:
SomeStruct(const int someInt) : m_someInt(someInt)
{
}
private:
const int m_someInt;
};
More info: Constructors and member initializer lists
Value cannot be assigned to a const storage. It can be only initialized. In case of class member variable it would be done in initialization list.
struct SomeStruct
{
public:
SomeStruct(const int someInt) : m_someInt(someInt)
{
}
private:
const int m_someInt;
};
Sometimes in-class initialization is enough:
template <int Val>
struct SomeStruct
{
public:
private:
const int m_someInt = Val;
};
Usually confusion stems from the fact that programmer doesn't see difference between two cases:
// 1) Declaring storage of int object that initially contains value 5
int a = 5; // It's a declaration.
// 2) Declaring storage of int object that contains undetermined value
int b; // Declaration
b = 5; // Assigning a value to it. It's a statement.
In your case m_someInt = someInt; is a statement that expects lvalue before =, and m_someInt is not a legal lvalue because it is const.

What are the disadvantages of define a field (in class) as a reference? [duplicate]

This question already has answers here:
Should I prefer pointers or references in member data?
(9 answers)
Closed 4 years ago.
What are the disadvantages of defining a field (in a class) as a reference?
For example:
template <typename T>
class A {
T& x;
public:
//....
}
In addition, are there special things that I need to do while I define a reference member?
Let's look at an example that shows a disadvantage of having references as members of a class:
template <typename T>
A<T>::A(T & obj) : x(obj) {} // constructor (this would probably go in the header)
int main(void) {
int * num = new int;
*num = 42;
A<int> a(*num); // a.x references *num
delete num; // uh-oh, *num is no longer valid and a.x cannot rebind
return 0;
}
Of course, if the member variable was declared T x;, then x would be another instance of T capable of storing a copy the data of *num even after num is deleted.

C++ operator ->* and .* [duplicate]

This question already has answers here:
What are the pointer-to-member operators ->* and .* in C++?
(7 answers)
Closed 7 years ago.
Good day,
I've come across this question, but I'm specifically interested in the "object pointed to by member ..." type operators as listed here on Wikipedia.
I have never seen this in the context of actual code, so the concept appears somewhat esoteric to me.
My intuition says they should be used as follows:
struct A
{
int *p;
};
int main()
{
{
A *a = new A();
a->p = new int(0);
// if this did compile, how would it be different from *a->p=5; ??
a->*p = 5;
}
{
A a;
a.p = new int(0);
// if this did compile, how would it be different from *a.p=5; ??
a.*p = 5;
}
return 0;
}
But this doesn't compile because p is undeclared. (See example)
Could anyone please provide a real-world example of the use of operator->* and/or .* in C++?
Those operators are used for pointer-to-member objects. You won't come across them very often. They can be used to specify what function or member data to use for a given algorithm operating on A objects, for instance.
Basic syntax:
#include <iostream>
struct A
{
int i;
int geti() {return i;}
A():i{3}{}
};
int main()
{
{
A a;
int A::*ai_ptr = &A::i; //pointer to member data
std::cout << a.*ai_ptr; //access through p-t-m
}
{
A* a = new A{};
int (A::*ai_func)() = &A::geti; //pointer to member function
std::cout << (a->*ai_func)(); //access through p-t-m-f
}
return 0;
}
The ->* and .* syntax is the "pointer-to-member" operator that can be used to store pointers to members of a very specific object.
Usage example:
class A {
public: int x;
};
int main() {
A obj;
int A::* memberPointer = &A::b; //Pointer to b, which is int, which is member of A
obj.*memberPointer = 42; //Set the value via an object
A *objPtr = &obj;
objPtr->*memberPointer = 21; //Set the value via an object pointer
}

One member enums in C++ [duplicate]

This question already has answers here:
static const Member Value vs. Member enum : Which Method is Better & Why?
(6 answers)
Closed 10 years ago.
I stumbled upon this answer to a question about powers of integers in c++: https://stackoverflow.com/a/1506856/5363
I like it a lot, but I don't quite understand why the author uses one element enums as opposed to some integer type explicitly. Could someone explain?
AFAIK this has to do with older compilers not allowing you to define compile-time constant member data. With C++11 you could do
template<int X, int P>
struct Pow
{
static constexpr int result = X*Pow<X,P-1>::result;
};
template<int X>
struct Pow<X,0>
{
static constexpr int result = 1;
};
template<int X>
struct Pow<X,1>
{
static constexpr int result = X;
};
int main()
{
std::cout << "pow(3,7) is " << Pow<3,7>::result << std::endl;
return 0;
}