function argument

If i am given a class constructor like this: Class::Class(char* char), is there a way to call the function in the main program like this:

Class* new_object = new Class("string")?

It works when i do it like this:
char class[] = "something";
char* ptr = class;
Class* new_object = new Class(ptr);

but i am wondering if it can be done without the char array and pointers?
Hello necrophos,

First I would stop using the "char*"and use a "std::string".
Then you would have Class::Class(std::string str) and you will find it much easier to use in the end.

Post the code for the whole class. There might be something else worth talking about.

Andy
but i am wondering if it can be done without the char array and pointers?
Sure:
1
2
3
char class[] = "something";
char* ptr = class;
Class* new_object = new Class(ptr class);


If you'd make it const you can directly pass the string:
1
2
3
Class::Class(const char* char)
...
Class* new_object = new Class("something");


Note that you should copy the provided string since you cannot tell anything about its lifetime.
The thing is that "something" is a literal string constant.

There is a difference in:
1
2
const char* bar   = "hello";
      char  foo[] = "hello";

For the first the compiler does add the literal values of "hello" into the binary and stores the address (of 'h') into the (stack) object 'bar'.

For the latter the code allocates (stack) memory for 6-byte array and initializes the array elements (possibly from very same literal "hello") with values.

Both 'bar' and 'foo' can be used where-ever (const) char pointers are acceptable.

One more:
const char gaz[] = "hello";

The 'gaz' is an array (in stack), like the 'foo'. A "local copy".
Its elements are also const, immutable, like the characters of the literal "hello" that the bar points to.
Thank you all.
The problem is, for my homework, i need to use the provided constructor(it must be char* as an argument).

I am creating a Polynomial class and i need to extract coefficients and exponents from a string(for example: "3x^2+5x+3").
I created two arrays for storing the info about the polynomial but i have trouble going through the string and extracting the data i need. I need to include different cases: +(or -)before the first member of the polynomial, double-digit numbers.
My code works fine if i only use 1-digit numbers, but it doesn't work for double-digits.
i need to use the provided constructor(it must be char* as an argument).

Fine, but you should know more about that class.

Why does it take a pointer to non-const?
Will it (the object) modify the pointed to data?
Does it assume one char, C-string, or something?
Does it take ownership of the pointed to memory?

I am creating a Polynomial class and i need to extract coefficients and exponents from a string(for example: "3x^2+5x+3").

That is a separate challenge. The std::string and std::istringstream are useful. (And std::regexp.)

The input has one or more terms separated by + or -. A separator affects the term following it. There is no separator after the last term. There could be a separator before the first term

Each term contains coefficient. The 'x' is optional.
If there is 'x', then there could be '^'.
If there is '^', then there could be 'exponent'.
Can you have "7x^-3"? Can you have parentheses?

The point is that you should be able to split one string input into a list of terms (still strings) and then process each term separately (to create object(s) representing that term).
Well the class is about Polynomials and operations between polynomials. I need to implement a constructor - Polynomial::Polynomial(char* poly) which takes a string or whatever is appropriate. I think the argument can also be constant(const char* poly). The position of terms in a poly is sorted from highest exp to the lowest so i don't need to deal with that. Also, there can never be something like 7x^2-3, 0x^2+1, .4x^2+x...
I am only allowed to use functions from <string> and maybe <sstring>. I guess that <iostream> is not mentioned but expected.


by the way, Class new_object(ptr);
http://www.cplusplus.com/forum/general/138037/
Topic archived. No new replies allowed.