Error in structure pointer of C++: expression must be a modifiable lvalue_Finally found the solution is simple

I used to use pointers just fine.

For a long time (with C++ for 2 weeks), the class pointer is directly written in the class, and there is no problem with direct initialization

private:
	//D3D类
	D3DClass* mD3D;
mD3D = new D3DClass*

As a result, to do data conversion today, it must be a structure (there is also an advantage, it is better to write a header file than a class, and a CPP file writes two files and more classes, and the structure is easier to understand), and the result is crazy, and it is said that it cannot be modified. lvalue of


public:
    FrameBufferType* lastBuffer;

Searched the Internet, possible solutions include

1. Pointer assignment

int * p ;

p = (int *) 4;

p* = *xxx;

First of all, not to mention that in actual combat you will not use int at all, but

Second, I've actually seen a famous college student write such "complex" code

2. lvalue assignment

The left side of the equal sign is an lvalue, the right side is an rvalue, and the left side needs to be a variable,

1 = x;//这样肯定提示左值不可修改

I don't think anyone who's been in elementary school would say such nonsense.

People who go to CSDN are all smart people. They must initialize variables and use variables to store some data.

3. The initialization of the structure should be initialized like this

Of course, the above screenshot does not seem to have an initialization problem, but another problem.

Character arrays cannot be assigned to another array with "=", that is, name=str is not acceptable.

Not sure if it's the reason I compiled with visulstudio,

If we write it like this, even if there is no array problem, the initialization of the first line will not compile, and the prompt is:

Uninitialized variable is used

I didn't want to start talking about it, but after using C++ for two weeks, I found two important problems first.

1. Various include exceptions, can not find the file (separating the header file and CPP caused the pot)

2. The simplest pointer (reflection) judgment cannot be done, and the compilation must be compiled from top to bottom (and various macro flips cannot be avoided)

Well, these are not important, c++ is fast, powerful, programming ideas

(It was definitely better than C 40 years ago, it was also called c plus plus, generally called A+, it also has the name of foresight, called ++)

But I just want to ask,

1. Want a string without a string

2. To have an array without an array (isn't the above screenshot obvious)

3. Various pointers are not sure whether they are assigned or not assigned

What can C++ do?

Kudos to the platform, 25% of programmers are using it, the world's first language

(What I heard is that there is no silver three hundred taels here)

4. Tried the following 3 methods without success

	//1.使用了未初始化的局部变量 's'
	//FrameBufferType* s;
	//s->View = matrixPtr->View;
	// 2.可以的分配??
	//lastBuffer = malloc(sizeof(struct FrameBufferType));
    //3.局部变量确实可以(cpp内局部)
	//FrameBufferType* xx = (struct FrameBufferType*)malloc(sizeof(struct FrameBufferType));
	

Actually, there are 3 clues to the solution

pointers can be assigned

address can be assigned

The const pointer cannot be changed, because const means that it cannot be changed. The role of , on efficiency, overall operation, on team collaboration, on extension development and other functions are fundamentally affected, tasteless, is the most appropriate evaluation of const)

5. Finally, I suddenly discovered that it was so simple

class FrameCBuffer : public CBuffer
{
	FrameBufferType* lastBuffer = new FrameBufferType;

As long as you give a default value, it can only be defaulted in the header file,

It is true that the structure pointer should be declared in the header file, and the logical judgment of the compilation should be the same as const......

Remaining problem

  • Will it take up memory
  • Will compiling slow down?
  • Does the package experience increase?

Many great people on the Internet are waiting for the compilation, drinking coffee while waiting for the compilation, and reading + compiling for a long time. It seems that everyone thinks something is wrong, generally not correct

Guess you like

Origin blog.csdn.net/avi9111/article/details/123854012