I've got some questions about return types from functions...
I've written a few programs now in C++ and they are working fine but I'm not sure if they are really programmed properly or not. The only issue that I'm faced with is am I returning valid values/objects that are safe?
Like is it safe to return a string object from a function? Is it safe to return an object from a function? As I know it isnt safe to return a array pointer from a function as that piece of memory is concidered unsafe (unless that array has been defined in the object and is deleted in the objects deconstructor)...
Some examples:
#include <iostream>
#include <string>
using namespace std;
class HTMLpage {
public:
string out(string)
};
string HTMLpage:
ut(string str) {
str="<html>\n<body>\n"+str+"\n</body>\n</html>\n";
return str;
};
void main() {
HTMLpage pg;
cout << pg.out("Hello"
;
}
Is it possible to return a string object as in the above example and be safe or is a string object excatly the same as a char array? Or for that matter is it possible to return any type of user made object from a function? Or would I be better off defining in the object a string variable member and just using that instead ... eg:
class HTMLpage {
public:
void out(string);
string src;
};
void HTMLpage:
ut(string str) {
src="<html>\n<body>\n"+str+"\n</body>\n</html>\n";
};
I've been spoilt with higher level languages for too long :S
I've written a few programs now in C++ and they are working fine but I'm not sure if they are really programmed properly or not. The only issue that I'm faced with is am I returning valid values/objects that are safe?
Like is it safe to return a string object from a function? Is it safe to return an object from a function? As I know it isnt safe to return a array pointer from a function as that piece of memory is concidered unsafe (unless that array has been defined in the object and is deleted in the objects deconstructor)...
Some examples:
#include <iostream>
#include <string>
using namespace std;
class HTMLpage {
public:
string out(string)
};
string HTMLpage:
str="<html>\n<body>\n"+str+"\n</body>\n</html>\n";
return str;
};
void main() {
HTMLpage pg;
cout << pg.out("Hello"
}
Is it possible to return a string object as in the above example and be safe or is a string object excatly the same as a char array? Or for that matter is it possible to return any type of user made object from a function? Or would I be better off defining in the object a string variable member and just using that instead ... eg:
class HTMLpage {
public:
void out(string);
string src;
};
void HTMLpage:
src="<html>\n<body>\n"+str+"\n</body>\n</html>\n";
};
I've been spoilt with higher level languages for too long :S