Casting return type

Hi!Ok so look at this code it's pretty simple, but I have the task to cast a return type.First here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <assert.h>
enum ObjectTypes
{
	DogType,
	CatType
};
class CAnimal
{
public:
	int m_MaxAge;
	int m_Age;
	CAnimal ();
	 virtual void Talk (void) {std::cout<<"Base animal doesn't talk!"<<std::endl;};
};
CAnimal::CAnimal () 
{
	m_MaxAge = 0;
	m_Age = 0;
}
 class CDog : public CAnimal
 {
 public:
	 CDog ();
   void Talk (void);
 };
  CDog::CDog ()
 {
  m_MaxAge = 7;
  m_Age    = 0;
 }
 void CDog::Talk (void)
{
       if (m_Age < m_MaxAge) std::cout << "Bark..." << std::endl;
       m_Age ++;
}
class CCat : public CAnimal
{
public:
	CCat ();
	void Talk (void);
};
CCat::CCat ()
{
 m_MaxAge = 5;
 m_Age    = 0;
}
void CCat::Talk (void)
{
	if (m_Age < m_MaxAge)  std::cout<<"Meow..."<<std::endl;
	m_Age++;
}
class CFactory
{
public:
	static CAnimal * GetType (int Type);
};
CAnimal * CFactory::GetType (int Type)
{
	switch (Type)
	{
	case DogType:
		return new CDog ();
		break;
	case CatType:
		return new CCat ();
		break;
	default:
		assert (0);
	    return 0;
	}
}
int main ()
{
	CDog * Dog;
	CCat * Cat;
    Dog = CFactory::GetType (DogType);
    Cat = CFactory::GetType (CatType);
	Dog->Talk();
	Cat->Talk();
	delete Dog;
	delete Cat;

	return 0;
}

I have to cast the return pointer of GetType (maybe to CDog and CCat but that can't be done) so that there're no errors in main with the function call.I know I can change types of Dog and Cat to parent class, but I just wonder if I can cast the return type of the function.How can I do it (if it can be done)?
Thanks in advance :)
Last edited on
static_cast and dynamic_cast.

1
2
3
Dog = static_cast<CDog*>( CFactory::GetType(DogType) );
// .. or
Dog = dynamic_cast<CDog*>( CFactory::GetType(DogType) );


dynamic_cast will do a runtime check to make sure what you're trying to cast is really a CDog, and will give you a null pointer if it doesn't. static_cast will assume that the cast is okay (even if it isn't!). dynamic_cast has a little more overhead than static_cast, but is safer. A bad static_cast could have disasterous consequences that are hard to find and solve.
Right I did that already and it didn't work, but when I copied your code it worked!Obviously when I tried it I missed the "*" after CDog and wrote <CDog> without the *.Thanks Disch :)
One more thing - can I make CFactory a singleton so that when using GetType second time it produces an error?I did that with variables but what about functions?
One more thing - can I make CFactory a singleton so that when using GetType second time it produces an error?I did that with variables but what about functions?


I don't follow you.

You mean like:

1
2
Dog = CFactory::GetType (DogType);
Cat = CFactory::GetType (CatType);   // you want a compiler error here? 


If that's what you mean, then no there's no way to do that.

You can make CFactory a singleton, but that implies something else, and wouldn't have much meaning here because CFactory only has static members anyway.
Sorry I didn't write it clear but yes that's what I thought.Actually I wanted to ask if there could be a singleton class implementation with functions (not with variables as it's supposed to), however as I read what I've written it does sound confusing (I was too sleepy ;D ) !Yet you answered my question!Thanks Disch for all your attention :) I think that's it (for now...) :)
Topic archived. No new replies allowed.