hello, i'm trying desperately to recover my c++ knowledge. I haven't done c++ for a year and now i'm facing some problems. Please help.
I.
How to type cast this:

class A { virtual c Read(); }
class B: public A {
public c Read(){
D d ;
return d;
}
}
class C {}
class D: public C {}
void main (){
B b;
D d = b.Read(); // how will i convert it??
}
}

i tried:

D * d = dynamic_cast<D*>(&b.Read());

but it gives "taking address of temporary" (section III)

II.
I write the following:

class A {
public A *succ;
}
void main(){
A a;
for(;a != NULL;a = a.succ); // EDIT: Multiple markers at this line
	- no match for ‘operator!=’ in ‘a != 0’
	- lvalue required as left operand of assignment
}
/*I don't need to overload the operators! They should work implicit, shouldn't they?*/

III.

/*Class structure is same as from section I*/
class A { virtual c Read(); }
class B: public A {
public c Read(){
D d ;
return d;
}
}
class C {}
class D: public C {}
void main (){
B b;
C *c = &(b.Read()); // error/warning: taking address of temporary?  !!edited!! > D * d is changed to C *c
}
}
/*Is this error serious? Will it delete itself? how can i get rid of the error?*/

Recommended Answers

All 9 Replies

This code :

class A { virtual c Read(); }
class B: public A {
public c Read(){
D d ;
return d;
}
}
class C {}
class D: public C {}
void main (){
B b;
D d = b.Read(); // how will i convert it??
}
}

Makes no sense. What is "c" before the Read function in class A.
Are you trying to practice polymorphism? What are you trying to do?

Yes, polymorphism. Something of X -> Y -> X .
sorry it's a type. It should be a capitol letter . "C"

Now i think i'm getting it. I think i should write

class A { virtual C *Read(); }
class B: public A {
public C *Read(){
D d ;
return &d;
}
}

Shouldn't have i? I'm gonna try it now.

Yep, It worked. No syntax errors. Hope no logical either.

And for the second section i've changed a to *a and a.succ to a->succ (obviousy).

Conclusion. Posting improves your memory and freshens you thinking. :)

>Yep, It worked. No syntax errors.
You'll find that people are more effective at helping you if you don't lie to them:

You: Here's my code, it compiles but gives me a warning "<blah blah>".
Us: That's funny, because the code you posted isn't and never has been legal C++. There's no way in hell that it would compile.

I'd suggest copying the code directly out of your text editor rather than trying to rewrite from memory when posting. You clearly aren't experienced enough with C++ to get it right without doing a test compile first.

I'd suggest copying the code directly out of your text editor rather than trying to rewrite from memory when posting. You clearly aren't experienced enough with C++ to get it right without doing a test compile first.

I didn't said it compiled. I said i'm having trouble/errors.

Or if you're referring to the lower posts... i was editing the code posted above. I don't actually struggle with the exact code written in the first post. I simplified it dramatically because the actual source code is rather complicated to understand.

>i was editing the code posted above
So we're supposed to use our mystical powers to "know" what you're doing without you having to post your progress? Sorry, but it doesn't work that way. I think you'll find that the most qualified people here will assume as little as possible. As such, what you post is what you have. Period. If you don't post your code in a way that accurately represents what you have and don't get helped, that's your fault. If you confuse the matter by giving updates on code that you haven't posted, that's your fault.

>I simplified it dramatically
Which brings us back to my original point. If you "simplify" your code so haphazardly that it becomes completely invalid, everyone will correct your code and not help you with your actual problem. It's easier to treat you like a beginner and correct syntax errors than play 20,000 questions to get the necessary information for addressing your stated problem.

>because the actual source code is rather complicated to understand
Your logic is flawed. Assuming you can come up with a question that's too complicated for all of us to understand, it's unreasonable to expect an answer. However, you'll find that this forum has several members who are extremely proficient in C++, so the original assumption that you can come up with a question that's too complicated is almost assured to be false.

Further, it's not difficult to pare down your code (if it's too long) into a smaller program that exhibits the problems you want help with, but is otherwise correct.

Unlike you, i'm not that fond of spending time on criticising somebody. Though i understand that your motives may come from wanting to help me for future posts. For that, i guess, i can thank you.

Hence, i don't feel like arguing (though i'd have smthing to say back). I don't know what is that illegal code you're referring to, but i don't neglect it has some errors(so i apologies for that). ie, i just noticed my functions are implicitly declared private. But i do believe they are not that big to make my point totally unclear. My motives for simplifying the code was to make it less frustrating reading it. And it was your assumption that i think all/everybody wouldn't be able to understand. As i already said, i wanted it to be easier to understand and closer to the core. It's not "fun" digging in a pile of .cpp and .h files.

I don't know what is that illegal code you're referring to,

How about the code in your first post?

But i do believe they are not that big to make my point totally unclear.

Apperantly, they are.

My motives for simplifying the code was to make it less frustrating reading it.

Although this is generally a good idea, simplifying your code to a point that it no longer functions, doesn't help your case. A lot of people who will try to help you, copy your code to their own IDE and try to run it. If it fails to compile, they fix it and reply what they've fixed to run your code. This does not answer your question at all, so all the trouble has been for nothing.
If you want to post a snippet of code that illustrates your problem, be sure that it compiles and indeed illustrates your problem.

>Though i understand that your motives may
>come from wanting to help me for future posts.

That's a first. Usually people throw tantrums at imaginary insults.

>though i'd have smthing to say back
You're welcome to do so. I'm reasonably confident that I can predict how such a debate would go.

>I don't know what is that illegal code you're referring to
Allow me to enumerate the errors then:

>class A { virtual C *Read(); }

  1. C doesn't exist at this point. You need to declare a type before using it.
  2. You didn't terminate the class definition with a semicolon.

>public C *Read(){
Member functions don't have an accessibility qualifier. You need to put a colon after "public" to initiate a public accessibility block in the class definition.

>return &d;
d is a local variable, it gets destroyed when the function returns. Thus you're returning a pointer to memory that you don't own. The warning you got from this appears to be a large factor in your original confusion.

>}
Class B isn't terminated by a semicolon either. This is required syntax, so I can only assume you haven't been using C++ for long.

>class C {}
Once again, a missing semicolon in the class definition.

>class D: public C {}
Yet another missing semicolon. You seem to want us to think you're not an absolute beginner, but your "simplified" code habitually makes beginner mistakes.

>void main (){
main returns int, not void. There's no excuse for using void main in C++ because main automagically returns 0 if execution falls off the end. The old C excuse of saving keystrokes doesn't apply anymore.

>}
You have a rogue closing brace at the end.

Here's the simplified code I would expect. I also included a solution to your conversion problem. That conversion is called a downcast, by the way, and it should be avoided when possible:

#include <iostream>

class A {
public:
  virtual ~A() {}
};

class B: public A {
public:
  virtual ~B() {}
};

A *Read()
{
  return new B();
}

int main()
{
  A *a = Read();
  B *b = dynamic_cast<B*> ( a );

  if ( b != 0 )
    std::cout<<"Successful cast\n";
  else
    std::cout<<"Expected a B\n";

  delete a;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.