Thread
:
hildon will be supported?
View Single Post
dannym
2010-09-04 , 12:12
Posts: 56 | Thanked: 31 times | Joined on Jul 2008 @ Austria
#
18
efa:
Use what you think is right. Programming languages aren't that important and if it comes to porting it from one low-level language like C to another low-level language like C++, they're all very similar anyway :-)
SpideyBR said:
>Since C is a subset of C++
No, it isn't anymore. But almost.
> you can design your UI with C++, maybe even with QTCreator or other tool, and then just add your algorithm source and header files and invoke then directly from C++.
True, to a point. C++ function declarations aren't that much different from C function declarations. The naming convention is different, though. And the standard data structures are all different.
>If you have in your C source/headers:
>void do_something(int myint, char *cool_array, float awesomeRate);
efa:
If you mix C and C++ code, you have to add the following stuff to your C header file:
#ifdef __cplusplus
extern "C" {
#endif
void my_function(int x); /* or whatever */
#ifdef __cplusplus
};
#endif
ONLY then you can #include the header file into a C++ source file. This is so it looks the C functions up using the C naming convention.
And only then, :
>You just call them in C++ with:
>do_something(i, charArray, 3.1);
And this isn't the most difficult thing.
The difficult thing is to call a C++ method from within a C function.
If you specify the name of a C++ method, it will pick up only the CODE BLOCK address.
A C++ object instance has the data part (called "this"), without it, you can't call any C++ method that stores data in its object.
So you have to carry around both the code address and the data address and get it in a C callback and once you do that, how is it any different from just using plain C structs in the first place?
And if you are in an C source file, C++ types are not there so you cannot specify the signature of the function to call and since there's no reflection, that would be it. The only practical to get that to work is to compile the "C" code in C++ as well, which technically makes it (really weird) C++ code.
On the other hand, if you specify a callback in a modern language, it will of course automatically carry both the code and the data pointers (and eventual curried arguments).
Note that Qt adds a lot of customizations (they even have their own *callback compiler*) on top of C++ to make it bearable.
The library "Boost" does as well.
However, this is bandaids on top of bandaids and in the end the C++ compiler will use up 500 MB RAM in normal operation and take long to compile anything. And the error messages look like they are from another planet.
SpideyBR said:
>C++ is C with powers, believe it!
C++ makes me despair for computer science.
If I want to have actual fun doing programming, I use Python.
And about the comment about memory management, Glib does reference counting memory management and the GObject system already is an object system. How does it get better than that? Does Qt do garbage collection? Add anything of value to the object system?
As for using Qt because it will be used in upcoming MeeGo devices, that's a valid reason. On the other hand, they have Gtk as well.
As for how C++ is preprocessed to C:
----------------- C++ ----------------------------
class S {
protected:
int m_common;
public:
S();
}; // note semicolon
class A : public S {
private:
bool m_flag;
public:
A();
void checkSomething();
bool getFlag() const;
}; // note semicolon
S::S() {
this->m_common = 42;
}
A::A() {
this->m_flag = false;
}
void A::checkSomething() {
...
this->m_flag = true;
}
bool A::getFlag() const {
return this->m_flag;
}
int main() {
A a;
a.checkSomething();
a.getFlag();
or
A* b = new A;
b->checkSomething();
b->getFlag();
return 0;
}
----------------- C ----------------------------
typedef struct {
int m_common;
} S; // note semicolon
typedef struct {
S super;
bool m_flag;
} A; // note semicolon
void Z1SZ4ctor(S* this) {
this->m_common = 42;
}
void Z1AZ4ctor(A* this) {
Z1SZ4ctor((S*) this);
this->m_flag = FALSE;
}
void Z1AZ14checkSomething(A* this) {
this->m_flag = TRUE;
}
int Z1AZ7getFlagC(A* this) {
return this->m_flag;
}
int main() {
A a;
Z1AZ4ctor(&a); // this is done automatically in C++
Z1AZ14checkSomething(&a);
Z1AZ7getFlagC(&a);
or
A* b = malloc(sizeof(A));
Z1AZ4ctor(b); // this is done automatically in C++ (and in Glib because of g_object_new)
Z1AZ14checkSomething(b);
Z1AZ7getFlagC(b);
return 0;
}
---------------------
Not in the example:
- As I said, C++ method pointers are really really weird, which is why C++ programmers avoid them usually and just hardcode the method calls (like C programmers do except for signals). Qt went out of their way to make them less-sucky by autogenerating code, this is called the Meta-Object compiler (MOC).
- Virtual functions
class A {
public:
A();
virtual void foo();
};
A::A() {
... your code
}
void A::foo() {
}
A a;
a.foo();
is C++-speak for:
struct A {
void (*Z1AZ3foo)(A* this);
};
void Z1AZ4ctor(A* this) {
this->Z1AZ3foo = Z1AZ3foo; // note that this is inserted into the actual constructor of the object where the new method is defined. This means that "foo" can still be NULL in below _even though_ it was defined and a method exists. Fun.
... your code.
}
void Z1AZ3foo(A* this) {
}
A a;
Z1AZ4ctor(&a);
a.Z1AZ3foo(&a); // NOT Z1AZ3foo(&a);
- C++ has exceptions. These are like setjmp() / longjmp(), but you can define automatic code blocks.
- C++ has templates which are like macros.
- the C++ macro language (templates etc) is (barely) turing complete while compiling, which means anything you could do at runtime you also can do at compiletime (note, nobody said it would look in any way readable :-) )
- C++ has namespaces which can be automatically prepended to your names. i.e.
using W = gtk_window;
W::new();
or whatever :-)
Last edited by dannym; 2010-09-04 at
13:44
.
Quote & Reply
|
dannym
View Public Profile
Send a private message to dannym
Find all posts by dannym