|
Hello everyone,
I need to work with C and C++ together in one application, for example, I need to call C++ methods from C method. And I have also used some namespace and other C++ features in my C++ method. So, is it possible to invoke C++ methods containing namespace (or other C++ specific features) from C?
I am using Visual Studio .Net. I am wondering how to make C and C++ work together. Are there any existing tutorials?
thanks in advance,
George
|
|
|
|
|
George_George wrote: So, is it possible to invoke C++ methods containing namespace (or other C++ specific features) from C?
not simply. you can put C++ inside a DLL or a COM object and let C call it that way. but otherwise, no. they are two different languages and you need some kind of language-neutral interface (ie. DLL/COM) for them to be able to interact.
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
Thanks man,
I think even if I put C++ inside a DLL I have to make a .h file for C code to compile. Then, I can link with the DLL. Is that correct?
So, the further question is that, I have to define a .h file for C++ methods for C, I am wondering how to do that? Do you have any samples?
regards,
George
|
|
|
|
|
George_George wrote: I have to define a .h file for C++ methods for C
you won't be defining C++ methods, you'll be defining C functions, and exporting only C functions. the DLL can use C++ internally, but the external interfaces need to be all C. if you need to pass objects out of the C++ code, you'll need to do it using "void *" or some other trick.
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
Thanks for your reply!
I have two more points,
1. I think to utilize C++ methods from C, I can also build C++ files into object files and then make C modules link with those object files. Is that method ok? Why do you say I have to build C++ methods into DLL?
2. Are there any existing samples to illustrate your methods/tricks?
regards,
George
|
|
|
|
|
1a. i don't know i've never tried.
1b. again, C and C++ are different languages and you need a language-neutral interface between them.
2. i don't have any small ones handy
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
Thanks!
My last question is that, if I want to use C++ functions from C. Do I have to build C++ functions into DLL then invoking functions from DLL? Can I just make a single project to include both C++ functions and C functions, and then build them altogether?
regards,
George
|
|
|
|
|
George_George wrote: So, is it possible to invoke C++ methods containing namespace (or other C++ specific features) from C?
Sure, as long as the C code is in a .cpp file. Just create an instance of the C++ object and use its methods just like you would from anyplace else.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
Thanks DavidCrow,
You mentioned "Just create an instance of the C++ object and use its methods just like you would from anyplace else.". Could you show me your points by a simple sample please? I do not quite understand what you said I can create an instance of a C++ class in a C file, since I can not imagine that C compiler can recognize C++ class.
Another question is that, why should I define C code in a .cpp file? I need to define it in .c file in my application.
regards,
George
|
|
|
|
|
George_George wrote: You mentioned "Just create an instance of the C++ object and use its methods just like you would from anyplace else.". Could you show me your points by a simple sample please?
If you have a class named MyClass , the following will create an instance of that class called mc :
MyClass mc; George_George wrote: I do not quite understand what you said I can create an instance of a C++ class in a C file...
The file will have to have a .cpp extension. Otherwise, the compiler will not recognize any of the C++ code contained within.
George_George wrote: ...since I can not imagine that C compiler can recognize C++ class.
Of course.
George_George wrote: Another question is that, why should I define C code in a .cpp file? I need to define it in .c file in my application.
You seem to have a bit of confusion between the two. C++ is everything that C is plus classes. So, I could put the following in a .cpp file:
void main( void )
{
int x = 5;
printf("%d\n", x);
return;
} Is that C or C++ code?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
Thanks,
DavidCrow wrote: You seem to have a bit of confusion between the two. C++ is everything that C is plus classes. So, I could put the following in a .cpp file:
void main( void ){ int x = 5; printf("%d\n", x); return;}
Is that C or C++ code?
My current situation is that, I am writing a C application to invoke C++ functions inside a DLL file. I do not have the source codes of the DLL file so that I can not add main method into C++ functions. As you mentioned that, I have to add main method to C++ modules. Do you have any ideas or comments to my situation?
Another point is that, if you think C and C++ are the same (or C is C++), then I think you can add main method to anywhere since C is C++. Why do you mean that I have to add main method to C++ module?
DavidCrow wrote: If you have a class named MyClass, the following will create an instance of that class called mc:
MyClass mc;
You mean if there is a public class in C++ and I can use the members/functions of this class in C directly by creating an instance of this class?
regards,
George
|
|
|
|
|
what is the diffrence between
return (SUCCESS) and return SUCCESS. And
Delete (this) and Delete this;
is it Only syntactic diffrence or any significent difference in it?
Guide me ?
krishna
|
|
|
|
|
It is just syntaxic difference. It is like in calculation:
1 + 1 is the same as
(1) + (1) and the same as
(1 + 1)
Sometimes it is needed to put '( )' but when it is not needed, there is no difference.
Or at least, no of what I'm aware of
|
|
|
|
|
|
1) Personally, I used to see some old pure C code in this way:
return (value);
And most pure C++ code in this way:
return value;
2) Another viewpoint, comparing with the casting, but I am not sure if there any relation between casting and return:
C and C++ allow either way to do casting:
(int)value
and
int(value)
3) One convenience to do return (value) would be:
You would feel safe to do more operations like:
return (value *2 +3);
Maxwell Chen
|
|
|
|
|
one precision Maxwell :
(int)value and int(value) are quite different.
(int) calls actually the overloaded int cast operator on the existing instance.
int() creates a new int instance calling the int copy constructor.
|
|
|
|
|
v2.0 wrote: (int)value and int(value) are quite different.
But, if the case that value is just of the built-in value types like char instead of class / struct / union types?
I am using VC++ 2003 now, and I tested the code just now:
char a;<br />
int b;<br />
b = int(a);<br />
b = (int)a;<br />
As indicated in the disassembly, they are the same as below:
char a;
int b;
b = (int)a;
0041F6BA cmp byte ptr [ebp-0E9h],0
0041F6C1 jne OnBnClicked+40h (41F6D0h)
0041F6C3 push 41F708h
0041F6C8 call @ILT+2635(__RTC_UninitUse) (419A50h)
0041F6CD add esp,4
0041F6D0 movsx eax,byte ptr [a]
0041F6D4 mov dword ptr [b],eax
b = int(a);
0041F6D7 cmp byte ptr [ebp-0E9h],0
0041F6DE jne OnBnClicked+5Dh (41F6EDh)
0041F6E0 push 41F708h
0041F6E5 call @ILT+2635(__RTC_UninitUse) (419A50h)
0041F6EA add esp,4
0041F6ED movsx eax,byte ptr [a]
0041F6F1 mov dword ptr [b],eax
Maxwell Chen
|
|
|
|
|
v2.0 wrote: int() creates a new int instance calling the int copy constructor.
int is an integral type, not a class. How can it have a constructor, or even a method?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
even intregral types can be considered - due to the fact that C++ is an OO language - like a class... of course, there are restrictions to this, but int() and (int) are not identical in theory...
|
|
|
|
|
v2.0 wrote: even intregral types can be considered...like a class
How so, and why would you want to? No OO-specific topics relate to them (e.g., they can't be inherited from).
v2.0 wrote: ...but int() and (int) are not identical in theory...
Which implies they are identical in practice. Yes?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: No OO-specific topics relate to them (e.g., they can't be inherited from).
i said there were limitation, not that all OO topics were applying.
DavidCrow wrote: Which implies they are identical in practice. Yes?
hum, looking at the assembler generated, i must agree that both are identical for the processor, but i still believe that it remains a compiler specificity
|
|
|
|
|
Page 70,
$ 5.2.3 Explicit type Conversion
1. A simple-type-specifier (7.1.5) followed by a parenthesized expression-list constructs a value of the specified type given the expression list. If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4). If the simple-type-specifier specifies a class type, the class type shall be complete. If the expression list specifies more than a single value, the type shall be a class with a suitably declared constructor (8.5, 12.1), and the expression T(x1, x2, ...) is equivalent in effect to the declaration T t(x1, x2, ...); for some invented temporary variable t, with the result being the value of t as an rvalue.
Maxwell Chen
|
|
|
|
|
ok, thanks Maxwell, that's more clear now.
|
|
|
|
|
Sometimes I need to display a bitmap much smaller then its original size, by using the StretchBlt API I get sadly ugly outcomes, I've noticed many picture-viewer applications have the ability of stretch-displaying pictures while maintaining high quality, some of them(IE 6.0, etc) can even do that job absolutely perfect. I wonder if I too can implement that in my own projects? Any help are appreciated.
|
|
|
|
|
for starters, see SetStretchBltMode
if that doesn't do it, you'll need to explore the world of image resizing algorithms and resize the image yourself. i hear there are toolkits out there to help with this.
=[ Abin ]= wrote: some of them(IE 6.0, etc) can even do that job absolutely perfect
no method is perfect. the best you can hope for, especially when enlarging, is adequate.
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|