簡體   English   中英

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;
}

我有一個Rectangle vector ,我有相同的 limits limitsumlimitmilti ,如何同時為所有對象設置這些限制,而不是分別為向量的每個 object 設置這些限制?

根據您的最后評論,如果我理解您想要什么,這是我的建議。

#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();

}

這將 output 得到以下結果:

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

請告訴我它是否符合您的要求,或者如果您有更正,請隨時告訴它以進行改進。

在評論中編輯您的問題:

實際上, static 成員是所謂的class variables (與instance variable相反),這意味着 class 的每個實例共享此變量的相同副本。 無論創建多少實例,每個 static 成員都只會有一份副本。

另外,請記住,只有 static 方法可以訪問static members (這就是為什么我將您的setlimits修改為static void setlimits(const double limitsum_, const double limitmilti_)

在下面的代碼中,我添加了對 static 變量值的修改,您會看到我不需要為每個實例修改值,只需為 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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM