• 14
• 12
• 9
• 10
• 13

Constructor not called if a default param is supplied?

This topic is 4654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, my question is simple: is the constructor for a class not called if it has a default parameter? I have a class:
namespace dftCommon
{
class StateMachine
{
Value*            m_pStates;
unsigned int    m_nCount;
public:
StateMachine(unsigned int nStateCount = 16); // Fixed
~StateMachine();

void    SetState(unsigned int nIndex, Value NewState);
Value    GetState(unsigned int nIndex);
void    SaveStates(Value* pStates, unsigned int nCount);
void    LoadStates(const Value* pStates, unsigned int nCount);
};
}


dftCommon::StateMachine::StateMachine(unsigned int nStateCount)
{
assert(nStateCount > 1);
m_pStates    = new Value[nStateCount];
m_nCount    = nStateCount;
memset(&m_pStates, 0, nStateCount);
}

dftCommon::StateMachine::~StateMachine()
{
delete m_pStates;
m_nCount = 0;
}

void dftCommon::StateMachine::SetState(unsigned int nIndex, Value NewState)
{
assert(nIndex < m_nCount);
m_pStates[nIndex] = NewState;
}

dftCommon::Value dftCommon::StateMachine::GetState(unsigned int nIndex)
{
assert(nIndex < m_nCount);
return m_pStates[nIndex];
return 0;
}

void dftCommon::StateMachine::SaveStates(dftCommon::Value* pStates, unsigned int nCount)
{
assert(pStates != 0);

// We only have enough states for an m_nCount, so we'll only fill that many.
if(nCount > m_nCount)
nCount = m_nCount;

for(unsigned int nIndex = 0; nIndex < nCount; ++nIndex)
pStates[nIndex] = m_pStates[nIndex];
}

void dftCommon::StateMachine::LoadStates(const dftCommon::Value* pStates, unsigned int nCount)
{
assert(pStates != 0);

// We only have enough room for m_nCount states, so we'll only get that many.
if(nCount > m_nCount)
nCount = m_nCount;

for(unsigned int nIndex = 0; nIndex < m_nCount; ++nIndex)
m_pStates[nIndex] = pStates[nIndex];
}


enum TEST_STATES { TEST_TITLE = 0 };

dftCommon::StateMachine g_StateMachine;
g_StateMachine.SetState(TEST_TITLE, "StateMachine Test");

I've stepped through it with my debugger, but the constructor code is never called. Did I miss something or is this a bug with MSVC++ 6? It stops on line 19 (the assertion in dftCommon::StateMachine::SetState().) [Edited by - Programmer16 on June 23, 2005 12:58:00 AM]

Share on other sites
try calling the constructor you created instead of the default one:

dftCommon::StateMachine g_StateMachine(1024);g_StateMachine.SetState(TEST_TITLE, "StateMachine Test");

I am not sure whether or not a default constructor with no arguments that does nothing gets created for you, or if all that happens is that the memory for your object gets allocated, but either way, your constructor will never get called if you dont provide the required arguments.

Share on other sites
I guess I should've figured that it would call the default constructor instead of the one with the default parameter. Thanks!

Edit:
Okay, I've changed it to this:
dftCommon::StateMachine g_StateMachine(16);

And my constructor is still not getting called.

I'm not getting any errors, warnings, nothing. And it goes to SetState() correctly.

Edit 2:
Okay, I was trying to run to cursor, but that didn't work so I put a breakpoint in my constructor. It gets called, I stepped through it and all of the data and everything was created correctly. I then did run to cursor in SetState(), but by then m_nCount is 0 somehow.

Edit 3:
Okay I fixed it. I've never used memset() before and I was calling memset(&m_pStates, 0, nStates); (similiar to ZeroMemory()).

[Edited by - Programmer16 on June 22, 2005 9:17:18 PM]

Share on other sites
You know, it would probably be wise to keep nStateCount on a member variable, to check for bounds and such and for walking the array if needed.

Or you could use a std::vector instead, or even better a std::stack, since it's a state machine.

Share on other sites
That code shouldn't compile - perhaps you are debugging an old executable?
If you declare a non-default ctor, it hides the default ctor unless you explicitly add it.

dftCommon::StateMachine g_StateMachine;

...should fail.

Share on other sites
Quote:
 Original post by Shannon BarberThat code shouldn't compile - perhaps you are debugging an old executable?If you declare a non-default ctor, it hides the default ctor unless you explicitly add it.dftCommon::StateMachine g_StateMachine;...should fail.

No, this is code I wrote today. I thought that it should fail too, but maybe its just a bug in VC++ 6 or something. But I thought it would call the constructor since I supplied a default value for nStateCount.

@Kwizatz: I do store the number of states: m_nCount. I also do bounds checking (assert(nCount < m_nCount); etc.)

Anyway, I've done away with this code (sent it to the archive), since I've decided that its not needed. Thanks for the help guys.

Share on other sites
maybe i'm blind, but where is the default value you talk about?
Perhaps you copied an old version?
As written you never supplied the default value.

Share on other sites
Why don't you use the debbuger and step into
dftCommon::StateMachine g_StateMachine(16);
to see what gets called.

Share on other sites
Quote:
 Original post by Amnestymaybe i'm blind, but where is the default value you talk about?Perhaps you copied an old version?As written you never supplied the default value.

Oops, I posted the code after I removed it. I did have a default value of 16.

Quote:
 STLDudeWhy don't you use the debbuger and step intodftCommon::StateMachine g_StateMachine(16);to see what gets called.

Quote:
 Programmer16I've stepped through it with my debugger, but the constructor code is never called. Did I miss something or is this a bug with MSVC++ 6? It stops on line 19 (the assertion in dftCommon::StateMachine::SetState().)

Quote:
 Programmer16Okay, I was trying to run to cursor, but that didn't work so I put a breakpoint in my constructor. It gets called, I stepped through it and all of the data and everything was created correctly. I then did run to cursor in SetState(), but by then m_nCount is 0 somehow.

Okay guys, I figured out the problem. I was using memset() (instead of ZeroMemory(), which I usually use) incorrectly.

Thanks for the help!