gpt4 book ai didi

c++ - C++中类的常量

转载 作者:行者123 更新时间:2023-12-04 07:54:16 27 4
gpt4 key购买 nike

假设我的 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;
}
我有一个 vectorRectangle我有相同的限制 limitsumlimitmilti ,如何同时为所有对象设置这些限制,而不是分别为 vector 的每个对象设置这些限制?

最佳答案

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

#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 相反)这意味着该类的每个实例共享该变量的相同拷贝。无论创建了多少个实例,每个静态成员都只有一个拷贝。
另外,请记住只有静态方法可以访问 static members . (这就是为什么我将您的 setlimits 函数修改为 static void setlimits(const double limitsum_, const double limitmilti_)在下面的代码中,我添加了对静态变量值的修改,您会看到我不需要为每个实例修改值,只需为类修改一次。我在这里使用了 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;
}

关于c++ - C++中类的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66784474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com