Simple C++ question

Posted on 2013-01-28
Last Modified: 2013-01-28
Hello experts,

please have a look at the code below:

#include <iostream>
#include <cstring>
using namespace std;

//tworze klase, ktora umozliwi dodawanie stringow oraz
//zapis stringow w postaci zmienna = "jakis string";

class String{
	char * string;

	String(const char* aChar){
		string = new char[????];   how can I determine the length of aChar here?
		strcpy(string, aChar);

		delete string;
		string = 0;

	char * getString(){
		return string;

	void operator = (char * aString){
		strcpy(string, aString);

	friend ostream& operator<< (ostream & aOstream, const String & aString);


ostream& operator<< (ostream & aOstream, const String & aString){
	aOstream << aString.string;
	return aOstream;

int main(){
	String string1("kdjshfgkljs");
	String string2("skldjhfls");

	cout << string1 << string2;

	return 0;

Open in new window

Thank you

Question by:panJames
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 31

Accepted Solution

Zoppo earned 500 total points
ID: 38827233
Hi panJames,

you can use strlen, i.e.:
string = new char[strlen(aChar)+1]; // need to add 1 for terminating NUL character

Open in new window

Hope that helps,

LVL 31

Expert Comment

ID: 38830005
BTW, two more points:

1. In the constructor you should check if the passed string is a valid pointer.
2. The assignment operator isn't correctly implemented, it doesn't (re-)allocate the memory needed.

So I would change these two functions to look like this:
	String(const char* aChar){
		if ( aChar != 0)
			string = new char[strlen(aChar)];
			strcpy(string, aChar);
			string = 0;
			cout << "empty string"<< endl;

	void operator = (char * aString)
		delete [] string;
		string = 0;

		if ( aString != 0)
			string = new char[strlen(aString);
			strcpy(string, aString);

Open in new window


Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 78
C++ assignment question 7 268
Problem with SqlConnection 4 184
passing data from one form to another form in c++ 27 89
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question