How can I return a constant char within class?

I've made an char object variable called 'blockname'. I would like to call 'getblockname()' and return its value but there is complications with it being a const variable.

What is needed to let me return it?


// map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef MAP_H_
#define MAP_H_

class map
{
public:
	void 	setblockname(const char * blocknewname);
	char	getblockname();  // this is essentially what I want to do
private:
	char 	blockname[30];
};

#endif 


// map.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "map.h"

// This function works fine:
void map::setblockname(const char * newblockname)
{
	// convert name from string to char, add null at end
	strncpy(blockname, newblockname, 29);
	blockname[29] 	= '\0';
	
}

// this is what I want to do but doesn't work:
char map::getblockname()
{
	return blockname;
}
blockname is a char[30] but the return value is supposed to be a char. Return a const char* instead.
Last edited on
blockname is a char[30] but the return value is supposed to be a char. Return a const char* instead.

Where would I put the const char* ? I've tried putting this in many places with no success.
That's supposed to be the return value of the method.
Worked. Thank you. :)
Topic archived. No new replies allowed.