So, whats the thing with classes having "*" in their name ( for instance, classes without .cpp file dont have asterix, all others do)???
You definitely need to learn about pointers. test *
and test
are two completely different types in C++. Here's two variables with those types:
test t;
test* p;
Here, t
has type test
, and p
as type test*
. We describe test*
as "pointer to test
".
You can often think of a pointer as being the memory address of an object. So in p
, since it is a pointer, we could store the memory address of t
, which is a test
. To get the address of an object, we use the unary &
operator, like so:
test t;
test* p = &t;
Note that t
is a test
object. You didn't need to say new test()
. This is where C++ differs from other languages that you might have used, like C# and Java. In the above C++ code, t
is a test
object.
However, you can create objects with new test()
, so what's the difference?
test t;
creates a test
object with automatic storage duration. This means it is destroyed at the end of its scope (often the function is being declared within).
new test()
creates a test
object with dynamic storage duration. This means you have to destroy the object manually, otherwise you'll have a memory leak. This expression returns a pointer and so you can initialise a pointer object with it:
test* p = new test();
So now let's look at your problem:
test t=new test("rrr",8);
We now know that new test("rrr", 8)
returns a pointer to test
(a test*
). However, you're trying to assign it to a test
object. You simply can't do this. One of them is an address and the other is a test
. Hence the compiler says "no suitable constructor exists to convert from test *
to test
." Makes sense now, doesn't it?
Instead, you should prefer to use automatic storage duration. Only use new
if you really really need to. So just do:
test t("rrr", 8);