Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-20-2005, 01:05 PM   #1 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Initializing char array

I recently changed a string variable in my class to a character array. Before I did this, I was able to initialize it in my default constructor like so:

Code:
MyClass::MyClass() : 
myString(0)
{}
but now I get an error: incompatible types in assignment 'int' to 'char[2]'.

How would I fix this?

Thanks!
Aniviel833 is offline   Reply With Quote
Old 07-20-2005, 01:23 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
You'r char* which you've probably made it to, dosn't know how to be initialized with teh () operator.
You could do somethign like this:
Code:
MyClass::MyClass() : 
myString[0]='\0';
{}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-20-2005, 02:53 PM   #3 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Thanks for your reply.
I get errors when I try and use
Code:
MyClass::MyClass() : 
myString[0]='\0';
{}
but this works fine:
Code:
MyClass::MyClass()
{myString[0]='\0';}
I assume that's what you meant?

However, now what do I do with my normal constructor?
This works fine:
Code:
MyClass::MyClass(char m[2])
{for(int i = 0; i < 2; ++i){myString[i] = m[i];}}
But in another post Valmont (My question and his reply) explained the advantages of initializing instead of assigning, in constructors. Am I not now assigning the contents of m to myString? Is there a better way to do this? And are we assigning in the default constructor above, or is it different with values rather than passing a variable?

Thanks!
Aniviel833 is offline   Reply With Quote
Old 07-20-2005, 06:05 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
This is better and should be done:

The header:
Code:
//Implicit conversion and default constructor.
MyClass(const char* = "");
The implementation:
Code:
#include <cstring>

MyClass::MyClass( const char* s )
   : m_pChars( new char[ strlen( s ) + 1 ] )
{
  std::strcpy( m_pChars, s );
}
Don't forget the destructor!

The wrong code:
Code:
MyClass::MyClass() : 
myString[0]='\0';
{}
You may NOT use "=" here. You must use the parenthesis to explicitly denote the initialization in a constructor. Outside the (copy)constructors, you may use "=" to denote initialization indeed.

There is a second major topic here:
Your code (and Redhead's) implies that myString has a fixed size set in as a data member in the header. But this is illegal for arrays UNLESS it is a static member. LEGAL code below:
Code:
#include <cstring>
#include <iostream>

class MyClass
{
public:
  static char myString[5];
};

char  MyClass::myString[5] = "test"; //OR "" to initialize it as empty.

int main()
{
  std::cout<<MyClass::myString<<std::endl;
  std::cin.get();
  return 0;
}
Conclusion
Use my method (the very first example).
__________________

Last edited by Valmont; 07-20-2005 at 06:55 PM.
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Adding a CHAR to the end of a STRING MicroBlaze Standard C, C++ 3 06-11-2005 11:06 AM
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
Different string types in C: char pointer, char array, etc. Sleep Standard C, C++ 0 01-23-2005 08:40 PM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM
more help needed kashif Standard C, C++ 0 04-21-2003 10:30 AM


All times are GMT -8. The time now is 05:15 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting