class Interpreter
{
public:
Interpreter();
private:
std::vector<int> dumbList;
Lexer lexer;
Parser parser;
};
Interpreter::Interpreter() : lexer(lcparam1,lcparam2,…), parser(pcparam1,pcparam2,…)
{
}
int i = 10;
char *c = (char*) i; // fixed
#include <string>
void fn(const std::string &foo);
int main()
{
fn("Hello, world!");
char *charstarfoo = "Hello, world!";
fn(charstarfoo);
std::string stdstringfoo = "Hello, world!";
fn(stdstringfoo);
}
#include <string>
void fn(const char *foo);
int main()
{
std::string stdstringfoo = "Hello, world!";
fn(stdstringfoo.c_str());
}
It seems that when you create a class, and want to embed that into another custom class, it is a bit more complicated using
a reference than a pointer. If I wanted to add an STL object like a vector or list, I could just do it this way:
But say I wanted each Interpreter to have a Lexer and Parser object? How would I go about that? Would it make more sense
to instantiate the objects first, then in the constructor send them by ref? Or, could I initialize them within the constructor and
assign them afterwards? I know I could just use a pointer, but it doesn't make much sense considering that the Lexer and
Parser objects will never change, and therefor the variable representing them shouldn't change as well. I'll throw this out there
too (since it's almost on topic).
Strings in C++ elude me a bit as well. If I were to have a object constructor take a string literal as a parameter, I would assume that it would be received as a string. But whenever I do that, it says "cannot convert const char* to std::string". So,
I understand the whole C-Legacy deal, but this makes it a bit cumbersome. Must one create a string object and then pass it
by reference just to appease the C++ gods? I've observed that I could overload the constructor and receive a const char* which
could call the std::string constructor and convert, but that seems a bit awkward.