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;
}
Discussion:
19 comments Page 2 of 2.
Jay said:
1 decade ago
We can't use * as pointer in CPP but instead of this we can use reference keyword.
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.
Rakesh said:
1 decade ago
void(*ptr)() = &Bix::MyFunction;
What this line do?
What this line do?
Jayendrashirsat said:
1 decade ago
:: what does this operator do?
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.
Pavan said:
10 years ago
What is mean by linker error?
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;
}
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)
Prem said:
9 years ago
Linker error because there is no function definition for MyFunction().
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"
}
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"
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers