473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Creating an Object in a C function

I have a code like this
extern "C"
App * GetApp()
{
App a = new App;
a->Init();
return a;
}

Where App is a Class having some data members and accessor/modifier
function.

Is this allowed, i mean can i create an object inside a C function, is there
any ill effect of the code.
Jul 23 '05 #1
4 1195

"Sandy" <a@a.com> wrote in message
news:d9********@netnews.proxy.lucent.com...
I have a code like this
extern "C"
App * GetApp()
{
App a = new App;
a->Init();
return a;
}

Where App is a Class having some data members and accessor/modifier
function.

Is this allowed, i mean can i create an object inside a C function, is there any ill effect of the code.

I missed the *,
The correct line is
App *a = new App;

Jul 23 '05 #2
Sandy wrote:
"Sandy" <a@a.com> wrote in message
news:d9********@netnews.proxy.lucent.com...
I have a code like this
extern "C"
App * GetApp()
{
App a = new App;
a->Init();
return a;
}

Where App is a Class having some data members and accessor/modifier
function.

Is this allowed, i mean can i create an object inside a C function, is


there
any ill effect of the code.


I missed the *,
The correct line is
App *a = new App;


Yes, it is allowed. You don't have "a C function". You have a C++
function that has "C" _language_linkage_. (I presume you still compile
this code with a C++ compiler, don't you?)

V
Jul 23 '05 #3


Sandy wrote:
I have a code like this
extern "C"
App * GetApp()
{
App a = new App;
a->Init();
return a;
}

Where App is a Class having some data members and accessor/modifier
function.

Is this allowed, i mean can i create an object inside a C function, is there
any ill effect of the code.


Yes and no. Yes, this (with the correction you made in a followup) is
legal C++ code. It is not of course legal C.

If your intention is to call this from a C module, then it won't work.
That's because the C function won't be able to do anything with an
App*. That's the usual reason for this.
You need to give us more information about your overall design and
intentions to give a reasonable answer.

Brian

Jul 23 '05 #4
Default User wrote:

Sandy wrote:
I have a code like this
extern "C"
App * GetApp()
{
App a = new App;
a->Init();
return a;
}

Where App is a Class having some data members and accessor/modifier
function.

Is this allowed, i mean can i create an object inside a C function, is there
any ill effect of the code.

Yes and no. Yes, this (with the correction you made in a followup) is
legal C++ code. It is not of course legal C.

If your intention is to call this from a C module, then it won't work.
That's because the C function won't be able to do anything with an
App*. That's the usual reason for this.


The OP may not intend for the C code to do anything with the App*,
except pass it to other C-linkage functions as a "handle". Ferinstance:

APP.H:

// C++ code can just construct and manipulate App objects directly
#ifdef __cplusplus
class App
{
public:
App() {...}
Func1() {...}
Func2() {...}
~App() {...}
};

#endif

// C code can use these functions to access App objects
#ifdef __cplusplus
extern "C" {
#else
struct App;
#endif
App *CreateApp();
void AppFunc1(App *a);
void AppFunc2(App *a);
void DestroyApp(App *a);

#ifdef __cplusplus
}
#endif

APP.CPP:

#include "app.h"

App *CreateApp() {return new App;}
void AppFunc1(App *a) {a->Func1();}
void AppFunc2(App *a) {a->Func2();}
void DestroyApp(App *a) {delete a;}

--
Mike Smith
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dixie | last post by:
I wish to add some fields to an existing table in code. I am using the following code from rkc. CurrentDb.Execute ("ALTER TABLE MyTable ADD MyNewField Text 25") This works , but I need to also set...
6
by: Davinci_Jeremie | last post by:
Hi Newbee here to C# I have a simple questions... In a Hello world example how does the class object Hello exist with out creating it? I come from object pascal where everything object is...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
6
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
9
by: Brian | last post by:
I have a question that some may consider silly, but it has me a bit stuck and I would appreciate some help in understanding what is going on. For example, lets say that I have a class that...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
0
by: austincolby | last post by:
I am working on creating a SOAP client in Visual Studio 2008 for the first time but am running into a few issues. I added the Web Service reference to the project and am able to see the namespace...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.