| |

VerySource

 Forgot password?
 Register
Search
View: 938|Reply: 5

What a sentence on C ++ primer is so hard to understand!

[Copy link]

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

Post time: 2020-3-15 13:00:02
| Show all posts |Read mode
Which cattle person explain the following sentence

When a const static data member is initialized in the class body, the member must still be defined outside the class definition
But because the initial value of this static data member is specified in the class body, definitions outside the class definition cannot specify the initial value
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-6-13 13:45:01
| Show all posts
You write a piece of code to try it out.
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-13 21:15:01
| Show all posts
class foo {
static const int a = 5;
}
const int foo::a;
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

 Author| Post time: 2020-6-14 12:00:02
| Show all posts
thanks
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-6-15 17:45:01
| Show all posts
Static data members do not belong to class objects, but are shared by all class objects. You can understand it this way:
The static data member is independent of an object of the class, and its definition (allocating storage area) is not performed when the class object is defined, but independently.
The class definition itself does not allocate storage area, just introduce a type name (class type), so it must be defined outside the class definition, this is to allocate memory to static data members
class A
{
public:
static int i;
};

int main()
{
cout << A::i;
return 0;
}
If you write this, you will find that i does not exist

class A
{
public:
static int i;
};

int A::i =10;

int main()
{
cout << A::i;
return 0;
}
that's it
As for the initial value,
When it is pointed out in the class definition and when it is actually defined outside the class
Only one can be given. The routine above is given the initial value when it is actually defined.
Otherwise, how does the compiler know which one to use for initialization?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 18:00:01
| Show all posts
I found that the sentence on C++ prime seems to be wrong
When initializing a const static data member in the class body, the member must still be defined outside the class definition
But because the initial value of this static data member is specified in the class body, the definition outside the class definition cannot specify the initial value

I did this on VC++ 6.0 but there was an error
#include "stdafx.h"
#include<iostream>
using namespace std;
class A
{
public:
static const int i=10;
};
const int A::i;
int main()
{
cout<<A::i;
return 0;
}
Can that master help me explain it to me?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list