标签: member-variables

如何在成为类数据成员时调用指向成员函数的指针?

struct B
{
  void (B::*pf)(int, int);  // data member
  B () : pf(&B::foo) {}
  void foo (int i, int j) { cout<<"foo(int, int)\n"; } // target method
};

int main ()
{
  B obj;
  // how to call foo() using obj.pf ?
}
Run Code Online (Sandbox Code Playgroud)

在上面的测试代码中,pf是一个数据成员B.调用它的语法规则是什么?它应该是直截了当的,但我没有得到正确的匹配.例如,如果我尝试,obj.*pf(0,0);那么我得到:

error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘pf (...)’, e.g. ‘(... ->* pf) (...)’
Run Code Online (Sandbox Code Playgroud)

c++ member-function-pointers member-variables

11
推荐指数
3
解决办法
6142
查看次数

9
推荐指数
2
解决办法
1万
查看次数

C++ init-list:使用非初始化成员初始化其他成员不会发出警告

既没有g ++(4.4和4.6)也没有clang ++(3.2),也没有覆盖,-Wall和-Wextra(+其他一些)或-Weverything分别给出了以下代码片段的警告:

class B {
    char *t2;
    char *t;

public:
    B() : t2(t), t(new char[100]) {}
};
Run Code Online (Sandbox Code Playgroud)

我至少会期待一个关于未初始化(成员)变量使用的小警告.

有什么我想念的吗?这是一个想要的"无警告" - 场景.我(现在已经)我的软件中至少有一个很难找到的错误.

编辑:从这个新问题可以看出,我意识到在某些情况下,封面警告这个问题.

c++ gcc clang member-variables coverity

9
推荐指数
2
解决办法
304
查看次数

是否可以在Java中为具有接口成员变量的类编写复制构造函数?

如何为具有接口成员变量的类编写复制构造函数?

例如:

public class House{

    // IAnimal is an interface
    IAnimal pet;

    public House(IAnimal pet){
        this.pet = pet;
    }

    // my (non-working) attempt at a copy constructor
    public House(House houseIn){
        // The following line doesn't work because IAnimal (an interface) doesn't 
        // have a copy constructor
        this.pet = new IAnimal(houseIn.pet);
    }
}
Run Code Online (Sandbox Code Playgroud)

我被迫有一个混凝土Animal吗?如果是这样的话,似乎重复使用课程与狗的房子与猫的房子变得错综复杂!

java interface copy-constructor member-variables

9
推荐指数
1
解决办法
3914
查看次数

C++:引用/指向成员变量的指针作为模板参数

首先,我有这样的事情:

class Test {
    std::vector<int> a, b;
    void caller(...) { callee(...); }
    void callee(...) { /* Do stuff with 'a' */ }
}
Run Code Online (Sandbox Code Playgroud)

我想要的是拥有一个与callee 矢量完全相同的功能b.要做到这一点,有两个明显的解决方案:

  • 传递矢量ab作为参数.但是,callee一个递归函数可以进行数百次调用,并将向量作为参数传递只是不必要的开销.
  • 复制函数callee和使用向量b,这将是最好的选择,尽管这callee是一个相当长的函数,我会有很多重复的代码.

出于好奇,我去寻找模板部分,我注意到可以用于

左值参考类型

指针类型

指向成员类型的指针

所以我试着这样做:

class Test {
    std::vector<int> a, b;
    void caller(...) { callee<a>(...); }
    template <std::vector<int> &x> void callee(...) { /* Do stuff with 'x' */ }
}
Run Code Online (Sandbox Code Playgroud)

但我明白了

错误:在常量表达式中使用'this'

有没有办法用引用或指针实现这个?

顺便说一句,我想要的东西可以被视为功能范围 #define

c++ templates reference member-variables

8
推荐指数
1
解决办法
1246
查看次数

是否有可能在我的模板化类中有一个静态成员变量,而该类的用户不必知道它?

我有一个模板化的容器类,类似于这个玩具代码:

template <class ItemType> class MyVector
{
public:
   MyVector() : _numItems(0), _items(NULL) {/* empty */}

   /** Returns a reference to the first item in our array,
     * or a default-constructed item if the array is empty.
     */
   const ItemType & GetFirstItemWithDefault() const
   {
      return (_numItems > 0) ? _items[0] : _defaultItem;
   }

   [other methods omitted because they aren't relevant]

private:
   int _numItems;       // how many valid items (_items) points to
   ItemType * _items;   // demand-allocated
   const ItemType _defaultItem;
};
Run Code Online (Sandbox Code Playgroud)

这个类非常方便使用 …

c++ static templates member-variables

7
推荐指数
2
解决办法
192
查看次数

具有首字母的成员变量的命名约定

我想知道有人会如何命名以首字母开头而不是单词的成员字段.

public class MyClass {

    // I know this is how it is for a member field with words.
    private String myString;

    // What about this String? It is representing the term "RV Scale" which is 
    // short for "Reclose Volts Scale."
    private String RVScale;

}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用private String recloseVoltsScale,但我宁愿保留RVScale.如果有人知道如果它的应该是rVScaleRVScale或别的东西,我会很感激的信息.

编辑:

那么,像这样的成员领域呢......

private int RV;
Run Code Online (Sandbox Code Playgroud)

java naming-conventions member-variables

7
推荐指数
1
解决办法
924
查看次数

始终返回成员变量getter的引用是一个好主意吗?

如果我有一个包含许多int,floatenum成员变量的类,那么将它们作为引用而不是副本返回它是否被认为是有效和/或良好的做法,并返回不应该进行更改的常量引用?或者我有理由将它们作为副本归还吗?

c++ getter class member-variables

5
推荐指数
1
解决办法
823
查看次数

奇怪的"候选人希望在构造函数中提供1个参数,0提供"

我在C++中创建一个简单的线程服务器应用程序,事实是,我使用libconfig ++来解析我的配置文件.好吧,libconfig不支持多线程,因此我使用两个包装类来完成"支持".点是,其中一个失败:

class app_config {
    friend class app_config_lock;
public:
    app_config(char *file) :
        cfg(new libconfig::Config()),
        mutex(new boost::mutex())
    {
        cfg->readFile(file);
    }

private:
    boost::shared_ptr<libconfig::Config> cfg;
    boost::shared_ptr<boost::mutex> mutex;
};
Run Code Online (Sandbox Code Playgroud)

从我的main.cpp文件调用时失败可怕:

app_main::app_main(int c, char **v) : argc(c), argv(v) {
    // here need code to parse arguments and pass configuration file!.
    try {
        config = app_config("mscs.cfg");
    } catch (libconfig::ParseException &e) {
        cout << "Parse error at line " << e.getLine() << ": " << e.getError() << endl;
        throw;
    } catch (libconfig::FileIOException &e) {
        cout << "Configuration …
Run Code Online (Sandbox Code Playgroud)

c++ construction exception-handling member-variables function-try-block

5
推荐指数
1
解决办法
9447
查看次数

派生类中的“重命名”成员变量

这个问题似乎有点怪异,但我将尝试对用例进行一点争议。

假设您有一个二维(笛卡尔)字段的通用实现,该字段的尺寸为dim1和dim2,坐标为c1和c2。

struct Field2D {
  struct size_type {
     size_t dim1,dim2;
  };
  struct coordinates {
     size_t c1,c2;
  }
Field2D(size_type const & siz) : size_(siz), data(size_.dim1*size_.dim2) {}
double get(coordinates const & coords) {
  return data_[coords.c1*size_.dim2 + coords.c2];
}
//Other methods
private:
  size_type size_;
  vector<double> data_;
};

Run Code Online (Sandbox Code Playgroud)

您想从此类继承,但您想更明确地了解坐标的性质。例如,一个字段PositionVersusTime,其坐标为“ x”和“ t”,尺寸为“ nx”和“ nt”,我想将其用法

int main(){
   PositionVersusTime::size_type siz;
   siz.nx = 5;
   siz.nt = 2;
   PositionVersusTime myField(siz);
   PositionVersusTime::coordinates coords;
   coords.x = 2;
   coords.t = 0;
   auto out = myField.get(coords);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我要执行此操作的原因是,排序可能会根据字段(例如TimeVersusPosition)而改变,有时会被用户“遗忘”。有没有办法获得类似的行为?还是我应该简单地使用吸气剂(例如,coords.x()= 2)?

c++ member-variables

5
推荐指数
2
解决办法
113
查看次数