C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - General Questions (Q.No. 24)
24.
Which of the following statements is correct about the program given below?
class Bix
{
    public:
    static void MyFunction();
};
int main()
{
    void(*ptr)() = &Bix::MyFunction;
    return 0; 
}
The program reports an error as pointer to member function cannot be defined outside the definition of class.
The program reports an error as pointer to static member function cannot be defined.
The program reports an error as pointer to member function cannot be defined without object.
The program reports linker error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
19 comments Page 1 of 2.

Abhi said:   9 years ago
The linker error comes because the class is only declared and the definition is not there for the static member function.

If we give a definition for member function then the linker error will be resolved by adding the following code.

void Bix::MyFunction()
{
std::cout << "I am a static function of Class Bix"
}

Prem said:   9 years ago
Linker error because there is no function definition for MyFunction().

Legndery said:   9 years ago
Linker Error because when the compiler is searching for the original function to link to at void (*ptr)() = &Bix::MyFunction; it cannot find it. because the function is not defined.
this below example works perfectly:
#include<iostream>
using namespace std;
class Bix
{
public:
static void MyFunction();
};
int main()
{
void (*ptr)() = &Bix::MyFunction;
(*ptr)();
return 0;
}
void Bix::MyFunction(){

cout<<"yay"<<endl;
}
(2)

Pavan said:   10 years ago
What is mean by linker error?

Divya said:   10 years ago
:: this is a scope resolution operator. The scope resolution operator helps to identify and specify the context to which an identifier refers, particularly by specifying a namespace.

Jayendrashirsat said:   1 decade ago
:: what does this operator do?

Rakesh said:   1 decade ago
void(*ptr)() = &Bix::MyFunction;

What this line do?

Srinivas said:   1 decade ago
Linker not able to find the definition for static function so throwing an an error. Not having a definition is not a semantic error so compiler was not finding it.

Jay said:   1 decade ago
We can't use * as pointer in CPP but instead of this we can use reference keyword.

Uday said:   1 decade ago
If you receive a linker error, it means that your code compiles fine, but that some function or library that is needed cannot be found. This occurs in what we call the linking stage and will prevent an executable from being generated. Many compilers do both the compiling and this linking stage.


Post your comments here:

Your comments will be displayed after verification.