Character Array & Initialization Lists

K

Kris

Hi All,

I just tried to do something that I thought would be quite simple in C++ and
discovered (I think) that it's not possible. I did a bunch of reading and
everything that I've seen seems to indicate that it's not possible but I
thought I'd throw it out to all you c++ gurus to see what you say.

Is there any way to initialize a character array in the initialization list
for the constructor a class? If this is not possible, can anyone explain why
c++ doesn't allow this? I've included sample code below to demonstrate what
I'm trying to do. Thanks in advance for any responses provided.


The Code
============
class myClass
{
public:
myClass(); // contructor
char myCharArray[100];
};

myClass::myClass() : myCharArray("")
{
//some other stuff here
}

The Compile Error
==============
error C2536: 'myClass::myClass::myCharArray' : cannot specify explicit
initializer for arrays
 
E

EventHelix.com

To the best of my knowledge this is not possible in C++. A constructor
cannot be specified for an array.

You can use the STL string or a similar class to accomplish this.
Try wrapping your char array into an object with a constructor
that accepts a const char *. Declare this object instead of the
char array in your original object.

Sandeep
 
K

Kris

EventHelix.com said:
To the best of my knowledge this is not possible in C++. A constructor
cannot be specified for an array.

You can use the STL string or a similar class to accomplish this.
Try wrapping your char array into an object with a constructor
that accepts a const char *. Declare this object instead of the
char array in your original object.

Sandeep

Thank-you for taking the time to reply to my post Sandeep.

Unfortunately, I don't think that I can wrap the char array because the
class that I am trying to do the initialization in is being used for ADO
recordset binding. I know I didn't specify that in my original post but I
tried to simplify the example in the posting. I am definitely not an expert
with ADO but, from what I gather, you can only use basic C-style data types
when you are doing recordset binding. A shortened version of the actual
class is shown below in case you want to have a look at it. In the example,
the m_szStudyInstanceUID variable is the one that I would like to
initialize. Although not as elegant as I would like, I'm currently using an
assignment statement in the constructor rather than an initialization list.

class myCStudiesRecordSet : public CADORecordBinding
{
BEGIN_ADO_BINDING(myCStudiesRecordSet)
ADO_VARIABLE_LENGTH_ENTRY2( 1, adVarWChar, m_szStudyInstanceUID,
sizeof(m_szStudyInstanceUID), m_ulStudyInstanceUIDStatus, TRUE)
ADO_FIXED_LENGTH_ENTRY ( 2, adDate, m_dtStudyDate,
m_ulStudyDateStatus, TRUE)
END_ADO_BINDING()

//Attributes
public:
myCStudiesRecordSet();
wchar_t m_szStudyInstanceUID[200];
ULONG m_ulStudyInstanceUIDStatus;
};

myCStudiesRecordSet::myCStudiesRecordSet() : CADORecordBinding()
{
//initialize variables
m_szStudyInstanceUID[0] = (wchar_t)'\0';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top