C++ 中 class 的常量

Constants of a class in C++

假设我的 C++ 代码中有以下 类

class Test{
    
    double limitsum;
    double limitmulti;

    public:
    void setlimits(double limitsum_,double limitmilti_){limitsum=limitsum_;limitmulti=limitmulti_}
    bool checksum(double a, double b){
      return(a+b<limitsum);
     }
    bool checkmulti(double a, double b){
      return(a*b<limitmulti);
     } 
};
class Rectangle{

double width;
double hight;
public:
Rectangle(double width_,double hight_){width=width_;hight=hight_}
Test testfunctions;
}

我有一个 vector of Rectangle,我有相同的限制 limitsumlimitmilti,我如何为所有对象设置这些限制同时,而不是分别针对向量的每个对象?

根据你最后的评论,如果我明白你想要什么,这是我的建议。

#include <iostream>
#include <vector>
#include <stdlib.h>     /* srand, rand */

class Test
{
public:
    static double limitsum ;
    static double limitmulti;

public:
    static void setlimits(double limitsum_, double limitmilti_)
    {
        limitsum=limitsum_;
        limitmulti=limitmilti_;
    }
    bool checksum(double a, double b)
    {
        return(a+b<limitsum);
    }
    bool checkmulti(double a, double b)
    {
        return(a*b<limitmulti);
    }
};

class Rectangle
{

    double width;
    double hight;
public:
    Rectangle(double width_,double hight_)
    {
        width=width_;
        hight=hight_;
    }
    Test testfunctions;
};

// initialization of static members should be in the source file (the cpp but not the hpp)  in case you have multiple file
double Test::limitmulti = 12.0;
double Test::limitsum = 250.0;

int main()
{
    std::vector<Rectangle*> testing;

    for (int i =0; i<10; i++)
    {
        Rectangle* r = new Rectangle((double)i, double(i+1));
        testing.push_back(r);
    }

    // let us print the limits for each of the rectangles in the vector
    for (Rectangle* r: testing)
    {
        std::cout << "rectangle adress " << r << " : testfunctions.limitmulti " << r->testfunctions.limitmulti << " and testfunctions.limitmulti " << r->testfunctions.limitsum << std::endl;

    }

   //to free the allocated rectangles
   for (auto p : testing)
   {
     delete p;
   }
   testing.clear();

}

这将输出以下结果:

rectangle adress 0x391a48 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391a68 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391a88 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391aa8 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391ac8 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391b10 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391b30 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391b50 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391b70 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250
rectangle adress 0x391ae8 : testfunctions.limitmulti 12 and testfunctions.limitmulti 250

请告诉我它是否符合您的要求,或者如果您有更正,请随时告诉它以便改进。

在评论中编辑您的问题:

事实上,静态成员就是所谓的class variables(与instance variable相对),这意味着class的每个实例都共享该变量的相同副本。无论创建了多少个实例,每个静态成员都只有一个副本。

此外,请记住只有静态方法可以访问 static members。 (这就是为什么我将你的 setlimits 函数修改为 static void setlimits(const double limitsum_, const double limitmilti_)

在下面的代码中,我添加了对静态变量值的修改,您会看到我不需要为每个实例修改值,只需为 class 修改一次。这里我使用了static void setLimits方法。

Test::setlimits(5.6, 300.1);

    // let us print again the limits for each of the rectangles in the vector
    for (Rectangle* r: testing)
    {
        std::cout << "rectangle adress " << r << " : testfunctions.limitmulti " << r->testfunctions.limitmulti << " and testfunctions.limitsum_ " << r->testfunctions.limitsum << std::endl;
    }