Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Assume that A is a user defined class (as shown below)

C++
class A
{
	DWORD m_a, m_b;
	BOOL m_bFlag;

public:
	A()
	{
		ZeroMemory(this, sizeof(*this));
	}

	A(BOOL bFlag)
	{
		m_bFlag = bFlag;
	}
};

void foo()
{
	A *p1, *p2, *p3;


	/* I know this will work */
	p1 = new A(TRUE);



	/* This should be fine too */
	p2 = new A[10];


	/* Combining these... is something like this possible? */
	p3 = new A(TRUE)[10];
	
	/*
	I want the overload A::A(BOOL) to be called instead of default constructor and this should happen with all the objects.
	Is there any way to accomplish this?
	*/
}


I want to pass TRUE to all instances.

I know there are alternatives like creating a seperate function that'll allocate and set the member variable to TRUE and finally return the base pointer.
But is it possible to do that without any workaround?
Posted

1 solution

yeswekey wrote:
But is it possible to do that without any workaround?

Nope.
The language syntax doesn't allow that. You must use a workaround (you may, for instance use only one overloaded constructor with one optional BOOL argument):
C++
A(BOOL bFlag = TRUE)
{
  //..
}
 
Share this answer
 

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