Reputation: 77
Suppose that I have the following classes in my C++ code
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;
}
I have a vector
of Rectangle
for which I have the same limits limitsum
and limitmilti
, how can I set these limits for all the objects at the same time, and not for each object of the vector separatly?
Upvotes: 2
Views: 92
Reputation: 2412
According to your last comment, and if I understand what you want, here is my proposition.
#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();
}
This will output the following result:
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
Please tell me if it corresponds to what you want or if you have a correction, feel free to tell it to allow for improvement.
Edit concerning your question in the comment:
Indeed, static members are what is called class variables
(in opposition to instance variable
) which means that every instance of the class shares the same copy of this variable. There would only be one copy of each static member, regardless of how many instances are created.
Also, remember that only static methods can access static members
. (that's why I modified your setlimits
function into static void setlimits(const double limitsum_, const double limitmilti_)
In the code below, I added a modification of the values of the static variables and you see that I do not need to modify the value for each instance, only once for the class. here I used the static void setLimits
method.
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;
}
Upvotes: 2