C++ 如何使我的类成员函数不能在main中调用?

C++ 如何使我的类成员函数不能在main中调用?,c++,class,member,private-members,C++,Class,Member,Private Members,我正在为家庭作业编写几个类,我希望我的类成员函数不可能在main中调用。如果是,我希望程序退出。我如何知道何时调用我的成员函数?对于类,每个对象都表示一种颜色,格式为。谢谢你的帮助 class Color{ public: Color( unsigned red = 0, unsigned green = 0, unsigned blue = 0 ); // ctor unsigned getRed() const; // accessor unsign

我正在为家庭作业编写几个类,我希望我的类成员函数不可能在main中调用。如果是,我希望程序退出。我如何知道何时调用我的成员函数?对于类,每个对象都表示一种颜色,格式为。谢谢你的帮助

class Color{

public:
    Color( unsigned red = 0, unsigned green = 0, unsigned blue = 0 );    //  ctor
    unsigned getRed() const;    //  accessor
    unsigned getGreen() const;    //  accessor
    unsigned getBlue() const;    //  accessor
    Color & setRed( unsigned red );    //  mutator
    Color & setGreen( unsigned green );    //  mutator
    Color & setBlue( unsigned blue );    //  mutator
    const Color & output() const;
private:
    unsigned myRed;
    unsigned myGreen;
    unsigned myBlue;
    static unsigned okColor(unsigned color);

}; //Class Color

int main(int argc, const char * argv[])
{

}


Color::Color( unsigned red, unsigned green, unsigned blue):
myRed(okColor(red)),myGreen(okColor(green)),myBlue(okColor(blue))
{
    //initialization list here...
}

//accessors
unsigned Color::getRed() const {return myRed;}
unsigned Color::getGreen() const {return myGreen;}
unsigned Color::getBlue() const {return myBlue;}

//mutators
Color & Color::setRed(unsigned red){
    myRed = okColor(red);
    return *this;
}

Color & Color::setGreen(unsigned green){
    myGreen = okColor(green);
    return *this;
}

Color & Color::setBlue(unsigned blue){
    myBlue = okColor(blue);
    return *this;
}

//output 
const Color & Color::output() const{

    cout << "<" << myRed << "," << myGreen << "," << myBlue << ">" << endl;
    return *this;
}

//checkers
unsigned Color::okColor(unsigned myColor){

    if (myColor > 255) {
        die("Color intensity is out of range!");
    }

    return myColor;
}

bool die(const string & msg){

    cerr << endl << "Fatal error: " << msg <<endl;
    exit(EXIT_FAILURE);
}

Color mixture( const Color & color0, const Color & color1, double weight){

    double color1Multiplier = 0;
    Color mixture;
    unsigned mixtureRed;
    unsigned mixtureBlue;
    unsigned mixtureGreen;

    color1Multiplier = 1 - weight;

    mixtureRed = (color0.getRed() * weight) + (color1.getRed() * color1Multiplier);
    mixtureBlue = (color0.getBlue() * weight) + (color1.getBlue() * color1Multiplier);
    mixtureGreen = (color0.getGreen() * weight) + (color1.getGreen() * color1Multiplier);

    mixture.setRed(mixtureRed);
    mixture.setBlue(mixtureBlue);
    mixture.setGreen(mixtureGreen);

    return mixture;
}
类颜色{
公众:
颜色(无符号的红色=0,无符号的绿色=0,无符号的蓝色=0);//
unsigned getRed()const;//访问器
unsigned getGreen()const;//访问器
未签名的getBlue()常量;//访问器
Color&setRed(无符号红色);//mutator
Color&setGreen(无符号绿色);//mutator
颜色&setBlue(无符号蓝色);//mutator
常量颜色和输出()常量;
私人:
未签名的myRed;
未签名的myGreen;
未签名的myBlue;
静态无符号颜色(无符号颜色);
}; //类颜色
int main(int argc,const char*argv[]
{
}
颜色::颜色(无符号红色、无符号绿色、无符号蓝色):
我的红色(okColor(红色)),我的绿色(okColor(绿色)),我的蓝色(okColor(蓝色))
{
//这里是初始化列表。。。
}
//访问者
无符号颜色::getRed()常量{return myRed;}
无符号颜色::getGreen()常量{return myGreen;}
无符号颜色::getBlue()常量{return myBlue;}
//突变子
颜色和颜色::设置红色(无符号红色){
myRed=okColor(红色);
归还*这个;
}
颜色和颜色::设置绿色(无符号绿色){
myGreen=okColor(绿色);
归还*这个;
}
颜色和颜色::setBlue(无符号蓝色){
myBlue=okColor(蓝色);
归还*这个;
}
//输出
常量颜色和颜色::输出()常量{

cout防止从main调用类很简单。不要在
main
-无类->无法调用成员函数中创建类(除非它们是静态的)。您还可以将类头的
#include
移动到与
main
不在同一源中的某个文件中

不幸的是,没有(普通的和/或可移植的)方法来确定调用代码的函数[特别是如果我们记住,现代编译器经常移动代码,因此尽管您的代码是从main调用
mixed
,但编译器决定只将其移动到
main
,因为这会使它更快、更小,或者编译器具有内联函数的任何其他目标]


<>另一方面,没有任何方法可以阻止函数从访问对象的任何函数调用。对于函数的几乎每一个方面,<代码>主体< /C>都与其他函数没有区别。唯一的区别是,<代码>主< /代码>是从C++运行库中调用的。但是编译器并不真正关心你的FU。nction被称为
main
kerflunk
fred
如果您创建一个全局对象,它将在
main
之前初始化,并在
main
退出后销毁


你可以使用这个事实设置一个标志,并按照你的意愿行事。

但是你想从其他函数调用它们吗?是的。我只是想让它们不能在main中被调用。一种方法是将它们设置为私有,然后在
朋友
中创建你想访问它们的函数,如果它们不是太多的话。否则这会很难看。但到底是什么原因呢?与你的问题无关,但你真的需要学习如何使用。我再也不会使用
foo
bar
了。从现在开始,这将是
kerflunk
fred
!;)他不会试图阻止函数在
main
中被调用……他想ts不同行为(立即退出)如果有的话。@user1681673:我认为这是一个例子。你需要实现X,你认为实现X的方法是实现Y。然后你问如何实现Y。在这里,了解X对你来说很重要:你真正想要实现什么?如果从
main()调用函数有什么不好
,如果函数将从
bar()
调用则可以,而
main()
将调用
bar()
?@user1681673:如果从
main
调用函数,那么让函数表现得不同真的是你任务的一部分吗?这一要求似乎很奇怪。我已经编程30多年了,还没有任何要求去弄清楚“哪个函数调用了此代码”(除了调试程序中的崩溃转储和调用堆栈,但这有点特殊)。@user1681673:那么删除mutator函数
setRed()
setGreen()
,和
setBlue()就足够了
好吧,我假设OP希望对象在调用
main
后可用,而不是直接在
main
中使用。