Initializing const char * member variable in constructor

Hi all,

I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?

I want to do something like this, but I know this isn't allowed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* class.h */
class foo {
	public:
		foo(*parentWindowPtr);
		
	private:
		const char *name;
				
};

/* class.c */
foo:foo(*parentWindwPtr parentPtr) {
	name = parentPtr->windowName;
}


Any help is appreciated

- Weikardzaena

EDIT: I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class foo {
	public:
		foo(const char *parentWindowPtr);

	private:
		const char *name;

};


foo::foo(const char *parentWindwPtr):name(parentWindwPtr)
{
}
Ah yes thank you that worked, but is there a way to do it without initializer lists? I'm just curious.
but is there a way to do it without initializer lists? I'm just curious.

not that i know of.
Doing what you do in the first post should be fine because name is not const, it is what name points to that is const.

If you change name to a const pointer const char * const name; you must initialize it in the constructor initializer list as shown by Yanson.
Topic archived. No new replies allowed.