简体   繁体   English

在模板化类中折叠指针?

[英]Collapsing pointers inside a templated class?

Here is my code: 这是我的代码:

template<typename B>
class cvector{

public:

    cvector(){    
    allocatedSize =0;
    actualSize = 0;
    buffer = 0;
    }


    cvector(int k):allocatedSize(k){

    }


    cvector(int k, const B& initial ):allocatedSize(k), actualSize(k){

    buffer = new B[allocatedSize];

        for(int i = 0;i<allocatedSize;i++){
            buffer[i] = initial;
        }   
    }

    ~cvector(){
        delete [] buffer;                   
    }


private:
    int allocatedSize;
    int actualSize;
    B* buffer;

};

int main(int argc, char** argv) {

cvector<int> pee(2,int(5));
cvector<int*> kay(2,new int(10));
return 0;
}

I am trying to emulate std::vector . 我正在尝试模仿std::vector My code isn't optimized and clean yet as this is just a draft but as of now, i get no problems and here are my test cases when initializing: 我的代码尚未经过优化和清理,因为这只是草稿,但到目前为止,我还没有遇到任何问题,这是初始化时的测试用例:

cvector<int> p(2,int(5));                   //no error
cvector<int*> k(2,new int(10));             //no error
cvector<int> j(2,new int(5));               // error because it's int
cvector<int*> l(2,int(2));                  //error because it's a pointer

I am curious how are these B expressions evaluated if i will put int or int* ? 我很好奇,如果我将intint*放在这些B表达式如何评估?

B* buffer;                           // (int*)* so int *? (just like reference collapsing?
buffer = new B[allocatedSize];       // new int* if int* ?!

So if i wil use int* then B* buffer; 因此,如果我将使用int*则使用B* buffer; will be int* ? 会是int*吗? what about buffer = new B[allocatedSize]; buffer = new B[allocatedSize];怎么样buffer = new B[allocatedSize]; and const B& initial from my code? 和我的代码中的const B& initial

"Pointer collapsing" doesn´t exist. “指针崩溃”不存在。
No stars will be added or removed or anything else. 不会添加或删除星标或其他任何内容。

B* buffer;
buffer = new B[allocatedSize];  

with B being int results in B为int导致

int* buffer;
buffer = new int[allocatedSize];  

ie. 即。 an array of int , and you can do whatever you want with the int ´s. 数组int ,你可以做你想做与任何int的。

With B being int* , it will be B是int* ,它将是

int** buffer;
buffer = new int*[allocatedSize];  

ie. 即。 an array of int pointers, all pointers pointing nowhere (initially), 一个int指针数组,所有指针均无处指向(最初),
and you can do whatever you want with your pointers. 您可以使用指针做任何您想做的事情。

how are these B expressions evaluated if i will put int or int* ? 如果我将int或int *放在这些B表达式如何评估?

For int , the B will be int* and for int* , the B will be int** . 对于intB将为int* ;对于int*B将为int**

So if i wil use int* then B* buffer; 因此,如果我将使用int *,则使用B *缓冲区; will be int* ? 会是int *吗? what about buffer = new B[allocatedSize]; 缓冲区= new B [allocatedSize]怎么样? and const B& initial from my code? 和我的代码中的const B& initial

No. B* will be int** as mentioned above. B*int**如上所述。 For int* the expression would look like: 对于int* ,表达式如下所示:

(int**)buffer = new int*[allocatedSize];

The const B& initial would be evaluated as const int*& . const B& initial将被评估为const int*&

With my advice, for the production code you may think above having std::vector as an object and cvector as a wrapper around it. 根据我的建议,对于生产代码,您可能会在上面将std::vector作为对象并将cvector作为包装器作为对象。 Or inherit cvector from std::vector in a kind of "has a" relationship. 或以一种“具有”关系从std::vector继承cvector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM