Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I want to create an object that should not call my constructor..
C++
#include<iostream>
#include<conio.h>
using namespace std;
class me
{
public:
	me()
	{
cout<<"hi";
	}
	void fun()
	{
		cout<<"\nfun"<<endl;
	}
};
void main()
{
me b;
b.fun();
getch();
}

output:
hi
fun


My expected output is:
fun


What changes i have to make in that program to bring expected output..
The task is to create an object of that class and that should not call the constructor..
Posted
Updated 19-Oct-12 3:19am
v2
Comments
Argonia 19-Oct-12 9:16am    
well you can create another constructor with params that doesn't do anything or at least doesn't print anything.As far as i know when you create an object from a class depending on how you create it always the classes constructor is called. Even if you don't declare your own one the compiler adds one with no params(default constructor).My point is when you create object a constructor is always called :)
Richard MacCutchan 19-Oct-12 9:47am    
Remove the cout statement from the constructor.

Short answer: You cannot do that.
Longer answer: You cannot do that, is there any reason why do you want to do that?
 
Share this answer
 
Comments
J.Surjith Kumar 19-Oct-12 9:58am    
I got this question in the interview and they told that there is a technique to solve this.
Every class is required to have a constructor. You are not required to have it do anything useful. Write an empty constructor and move on.
 
Share this answer
 
Short answer: you cannot do that.

Cheating answer: make fun() static
C++
class me
{
public:
  me()
  {
    cout<<"hi";
  }
  static void fun()
  {
    cout<<"\nfun"<<endl;
  }
};
void main()
{
  b::fun();
  getch();
}


Dangerous, cheating answer: don't call the constructor
C++
void main()
{
  me* b = 0;
  b->fun();
}

This works, in this example, because the me class has no member data and no virtual functions (so no virtual function table as implicit member data). If I ever saw anyone submit code like this, I'd recommend they were sacked.

In an interview, they might have been interested in seeing if you had the understanding required to find this 'solution', and to say "In this case it would work, but I wouldn't do it for real".
 
Share this answer
 
Comments
J.Surjith Kumar 19-Oct-12 10:33am    
Thankyou!:) If the me class has member data and functions then how to do it!?
Orjan Westin 19-Oct-12 10:44am    
If the me class has member data, or another function which is virtual, the example would still work, as fun() is not trying to access any member data.

If fun() was virtual, or tried to access member data, you would have undefined behaviour, likely a application crash caused by memory access violation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900