【问题标题】:C++,Need help to understand some constructors and functions in a vector class using pointersC++,需要帮助理解向量类中使用指针的一些构造函数和函数
【发布时间】:2011-04-29 14:14:15
【问题描述】:

大家好;

我必须开发一个 C++ 类库,其中包含一系列用于科学计算的数值技术。该库应实现 Vector 类(使用指针),其中包含头文件“Vector.h”中所述的一些基本功能。

#ifndef VECTOR_H
#define VECTOR_H

template <class T>
class CVector {
private:
    int nn; //size of array
    T *v;   //pointer to array of data

public:

    //Default constractor
   CVector();

    //zero based array
    CVector(int n);

    //initialize to constant of value a
    CVector(int n, const T &a);

    //initialize to array a
    CVector(int n, const T *a);

    //copy constractor
    CVector(const CVector &rhs);

    //assignment
    CVector & operator=(const CVector &rhs);

    //i'th element
    inline T & operator[](const int i);

    inline const T & operator[](const int i) const;

    inline int size() const;

    //resize (contents not preserved)
    void resize(int newn);

    //resize and assign a constant value
    void assign(int newn, const T &a);

    //deconstractor
    ~CVector();

};

#endif  /* VECTOR_H */

我是 C++ 的初学者,对上面代码中的一些构造函数和函数的理解有些困惑。

我的问题是:

1- 下面的构造函数是什么概念?

    //initialize to array a
    CVector(int n, const T *a);

我的意思是如何将向量初始化为数组a?

2- 复制构造函数和赋值构造函数有什么区别?

    //copy constractor
    CVector(const CVector &rhs);

    //assignment
    CVector & operator=(const CVector &rhs);

3-我知道这个函数是返回向量的第i个元素:

    //i'th element
    inline T & operator[](const int i);

但是它和这个有什么区别:

    inline const T & operator[](const int i) const;

我需要了解这个概念才能知道如何在 .cpp 文件中实现它们以及如何在我的 main 中调用它们。如果你能帮助我,我会很高兴的。

最好的问候;

【问题讨论】:

  • 库 cmets 中似乎有一些拼写错误。当它说“初始化到数组 a”时,它可能意味着“初始化 from 数组 a”。另外,我不认为它被称为“contractor”,而是“constructor”。

标签: c++ pointers vector constructor inline-functions


【解决方案1】:

Q1:此构造函数可用于用数组中从 a 开始的 n 个元素的内容填充向量。

例如:

   float a[42] = { 31, 41, 59 };
   CVector<float> v( 3, a );

Q2:第一个是拷贝构造函数,第二个是赋值运算符。复制构造函数用于将值复制到函数参数中,从函数返回值,或初始化变量。

例如,复制构造函数用于这些:

CVector<float> foo( CVector<float> v ) { ... }

...
CVector<float> v1;
CVector<float> v2 = foo( v1 ); // Copy constructor used to pass in v1, and to return v2
CVector<float> v3 = v1; // Copy constructor used to copy v1 to v2.

而分配是用于此的:

CVector<float> v4;
v4 = v1;

第三季度。第一个可以用在作业的左侧。 const 版本在应用于 const 对象时使用。

void bar( const float & fval ) { ... }
...
CVector<float> v1( 3, a );
v1[0] = 42;   // Non-const operator[]

const CVector<float> v2 = v1; 
float value = v2[0];  // Const operator[]
bar( v2[0] ); // Const operator[]

【讨论】:

  • 对于你 Q3 的例子,非常量运算符 [] 就足够了。
  • 这两个都使用非常量版本:float value = v1[0];bar( v1[0] );
  • 感谢您的帮助,但我仍然没有得到Q3的答案。
  • @Daisy,一个是 const,另一个不是。 const版本允许你不修改访问,非const版本允许你修改。
【解决方案2】:

1) 将您的成员设置为:v = new T[n]; nn = n; 并复制元素:for (int i = 0; i != n; ++i) v[i] = a[i];

2) 复制分配是指您已经拥有一个对象,并且想要为其分配不同的值。复制构造函数是当您想要使用现有对象的值创建新对象时。

3) 在c++中有一个const函数的概念:如果你在一个const对象上调用函数;说:CVector&lt;int&gt; const&amp; x = ...; x[3] = 3; 不起作用,因为x 是常量。但如果这不起作用,operator[] 需要将const &amp; 返回到您的内部。
仅供参考:如果x 是非常量,则x[3] 的类型是T &amp;,因为使用了operator [] 的非常量版本。但是,如果 x 是 const,则 x[3] 的类型是 const T &amp;,因为使用了 operator [] 的 const 版本。

【讨论】:

  • 感谢您的帮助,但我仍然没有得到Q3的答案。
  • @Daisy 在第三季度写了一点,但我想现在为时已晚 :)
【解决方案3】:

好吧,我不是 C++ 大师,所以希望有人能更胜一筹……但这是我在这个主题上的 $.02。

  1. 遍历数组,将每个元素添加到向量中。我假设 n 是数组中的元素数?

  2. 复制构造函数和赋值构造函数之间的区别可能是语义上的。它们对向量的内部结构会产生相同的影响,但可以在不同的情况下使用。

  3. 这只是一个猜测,但我认为可变性是不同之处。第二个函数返回一个不可变的 T,这意味着它不能更改。第一个函数返回一个可变(可变)T。

【讨论】:

    猜你喜欢
    • 2014-09-03
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多