开发者

One overloaded function, yet the program doesn't see it that way

The following code has overloaded function CandyBarFunc. First prototype defines the function so that it modifies the value of 开发者_开发知识库a structure. Second prototype defines the function so that it just displays the content of a passed structure. The problem is that when I run the console program nothing appears on the screen except the Press Any Key... I tried to debug it and found out that first prototype works properly(I added the display functionality from the second prototype to the first one) becuase it modified and displayed the contents of the structure. So therefore it seems that overloading didn't work because the second function prototype doesn't get called during execution because nothing is displayed on the console screen. I'm not sure if the signaure is bad because the compiler does't complain about the ambigious function call. Did I miss something obvious in the code?

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
    char name[40];
    double weight;
    int calories;
};

void CandyBarFunc(CandyBar & astruct, const char * aname = "Millennium Munch", double aweight = 2.85, int acalories = 350);
void CandyBarFunc(const CandyBar & astruct);

int main(void)
{
    CandyBar MyCandyBar =
    {
        "Hi",
        1.5,
        456
    };
    cout << "1" << endl; 'little debug'
    CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar'
    CandyBarFunc(MyCandyBar, "Hello World Candy Bar", 1.25, 200); 'suppose to modify MyCandyBar
    CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar again'
    cout << "2"; 'little debug'
    return 0;
}

void CandyBarFunc(CandyBar & astruct, const char * aname, double aweight, int acalories)
{
    strncpy_s(astruct.name,aname,40);
    astruct.weight = aweight;
    astruct.calories = acalories;
    cout << "Name: " << astruct.name << endl; 'not suppose to be here, just for debug'
    cout << "Weight: " << astruct.weight << endl; 'not suppose to be here, just for _ debug'
    cout << "Calories: " << astruct.calories; 'not suppose to be here, just for debug'
}

void CandyBarFunc(const CandyBar & astruct)
{
    cout << "Name: " << astruct.name << endl;
    cout << "Weight: " << astruct.weight << endl;
    cout << "Calories: " << astruct.calories;
}

Exercise:

The CandyBar structure contains three members. The first member holds the brand name of a candy bar. The second member holds the weight (which may have a fractional part) of the candy bar, and the third member holds the number of calories (an integer value) in the candy bar. Write a program that uses a function that takes as arguments a reference to CandyBar, a pointer-to-char, a double, and an int and uses the last three values to set the corresponding members of the structure. The last three arguments should have default values of “Millennium Munch,” 2.85, and 350. Also, the program should use a function that takes a reference to a CandyBar as an argument and displays the contents of the structure. Use const where appropriate.


Since MyCandyBar isn't const, the compiler choses the first (reference to non-const) overload.

But seriously, if you want one function to set properties and another function to print them out, please don't abuse overloading by giving them the same name. Just name them differently, no more problems.

Also, in C++ we prefer std::string to fixed-size character arrays and character pointers.


Since MyCandyBar is not const, it will always try to use the function which accepts the non const CandyBar. You can force it to call the other function by casting it to const:

CandyBarFunc((const CandyBar &)MyCandyBar);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜