COM

Hi,

I'm trying to do something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	LPVOID pClassObj;

	if (condition 1)
	{
		CClass1 *pClassObj = new CClass1();
	}
	else if (condition 2)
	{
		CClass2 *pClassObj = new CClass2();
	}
	else
	{
		return ERROR
	}

	if (pClassObj == NULL) return ERROR;

	pClassObj->Method1();
	pClassObj->Method2();

	return SUCCESS;


But it can't compile ( pClassObj is not a class type )
Because CClass *pClassObj = new CClass(); is between braces I guess (out of scope). Is there a way to do something like this ?

Thanks
But it can't compile ( pClassObj is not a class type )
It won't work either because pClassObj is not initialised.

Is there a relationship between CClass1 and CClass2? For example, are they derived from a common base class?
Is there a relationship between CClass1 and CClass2? For example, are they derived from a common base class?


No. Well they are COM objects so they derived from IUnknown but they are unrelated to each other except for 2 methods of the same name.

I have a plan b which satisfy my needs but it would be a bit more elegant if it fits as plan a. I'm not a c++ guru so if it hits a limit I can do it some other way. Do not waste to much time on it ;-)

Thanks
Ok, you can do it, but the code won't look like that.

Clearly, you're expecting both objects to have Method1() and Method2() as the same offset. If not, it won't work, you'll call the wrong functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
IUnknown pClassObj = NULL;

if (condition 1)
{
	CoCreateInstance(/* args */, &pClassObj);
}
else if (condition 2)
{
	CoCreateInstance(/* args */, &pClassObj);
}
else
{
	return ERROR
}

if (pClassObj == NULL)
	return ERROR;

((Class1*)pClassObj)->Method1();
((Class1*)pClassObj)->Method2();
return SUCCESS;
Registered users can post here. Sign in or register to post.