【问题标题】:How can I make it impossible for my class member functions to be called in main?如何使我的类成员函数无法在 main 中调用?
【发布时间】:2013-04-11 22:25:56
【问题描述】:

我正在为家庭作业编写几个类,我希望我的类成员函数不可能在 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;
}

【问题讨论】:

  • 但是您希望能够从其他函数中调用它们吗?
  • 是的。我只是希望他们不能在 main 中被调用。
  • 一种方法是将它们设为私有,然后在friends 中创建您想要访问它们的函数
  • 如果他们不是太多的话。否则会很丑。但这究竟是什么原因呢?
  • 与您的问题无关,但您确实需要了解not to design classes

标签: c++ class member private-members


【解决方案1】:

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

不幸的是,没有(简单和/或可移植的)方法来确定哪个函数调用了您的代码[特别是如果我们记住现代编译器经常移动代码,所以尽管您的代码从 main 调用 mixture ,编译器决定将其移入main,因为这使它更快、更小或编译器具有内联函数的任何其他目标]。

除此之外,没有办法阻止从任何有权访问该对象的函数调用函数。对于功能的几乎每个方面,main 与其他功能没有什么不同。唯一的区别是 main 是从 C++ 运行时库中调用的。但是编译器并不真正关心你的函数是否被称为mainkerflunkfred

【讨论】:

  • 我再也不会使用foobar。从现在开始,它将是kerflunkfred! ;)
  • 他并没有试图阻止函数在 main 中被调用......如果有的话,他想要不同的行为(立即退出)。
  • @user1681673:我认为这是XY problem 的情况。你需要实现 X,而你认为的方法就是实现 Y。然后你问如何实现 Y。在这里,重要的是要知道 X 对你来说是什么:什么你真的想达到吗?如果要从main() 调用该函数有什么不好,而如果从bar() 调用该函数并且main() 将调用bar() 是可以的?
  • @user1681673:如果从main 调用该函数,它是否真的是你任务的一部分?作为一项要求,这似乎很奇怪。我已经编程了 30 多年,还没有任何要求弄清楚“哪个函数调用了这段代码”(调试器中的崩溃转储和调用堆栈除外,但这有点特别)。
  • @user1681673:那么删除突变函数setRed()setGreen()setBlue()就足够了
【解决方案2】:

如果您创建一个全局对象,它将在main 之前初始化,并在main 退出之后销毁。

您可以使用此事实来设置标志并按照您的意愿行事。

【讨论】:

  • 好吧,我认为 OP 希望该对象在 main 被调用后可用,而不是直接在 main 内使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多