Struct in C and C++

  • Thread starter vishnupriya.sureshbabu
  • Start date
V

vishnupriya.sureshbabu

Struct in C and C++ are same? Can Struct in C handle member functions?
 
N

Noah Roberts

Jaspreet said:
NO

Can Struct in C handle member functions?

NO

struct X
{

void (*f)();
};

....

void ConstructX(struct X * x)
{
x = &someFun;
}

It's just a lot more hand work.
 
O

osmium

Noah Roberts said:
struct X
{

void (*f)();
};

...

void ConstructX(struct X * x)
{
x = &someFun;
}

It's just a lot more hand work.

And it ignores the fact that the phrase "member function" has a particular,
definite meaning in the C++ language.
 
N

Noah Roberts

osmium said:
And it ignores the fact that the phrase "member function" has a particular,
definite meaning in the C++ language.

That's a pretty silly statement.

If you want to compare features of two different languages then use the
most generic terms available.
 
C

Cy Edmunds

Noah Roberts said:
That's a pretty silly statement.

If you want to compare features of two different languages then use the
most generic terms available.

Your construction is certainly not a member function. For instance it
doesn't have a hidden "this" pointer. The only correct statement is that C
doesn't handle member functions, period.

Cy
 
R

raylopez99

Noah said:
(e-mail address removed) wrote:
Yeah, I neglected that param. Long time since I used C for OO :p

Isn't it true that C++ compilers were first written in C originally?
Kind of like that paradymn where a tool making machine produces machine
parts that are more precise than any individual component making up the
tool making machine.

RL
 
G

Gernot Frisch

raylopez99 said:
Isn't it true that C++ compilers were first written in C originally?
Kind of like that paradymn where a tool making machine produces
machine
parts that are more precise than any individual component making up
the
tool making machine.


uhm... and the first C compiler was written in ASM originally, so...
 
G

Greg

Gernot said:
uhm... and the first C compiler was written in ASM originally, so...

Actually the first C++ compiler was itself written in C++ - which might
sound like a chicken-and-egg impossibility were it not for one fact:
the first C++ compiler (CFront) did not actually compile C++ source
code (including its own) directly - rather it first translated C++
source code into C and then compiled the translated sources with a C
compiler. Pretty simple, isn't it?

Greg
 
V

Victor Bazarov

Greg said:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first translated
C++ source code into C and then compiled the translated sources with
a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are pretty
much the same thing. So, how could it have been written in C++? What
would be used to translate its own code from C++ "into C"? And if it
wasn't used itself, what was used? And if, in fact, some other thing
was used, what do you call it if not "C++ translator"? And what was
that thing written in? Catch my drift? There is no "chicken and egg"
problem. The "chicken and egg" conundrum exists when there is circular
dependency. There is none here. First C++ compiler/translator was
most likely written in C (or C With Classes or some other predecessor
of the "real C++"). After than each next version of a C++ compiler is
written in C++ probably (because the C++ compiler programmers know C++
the best), and any new advanced features the next version implements
are simply not used to write it (because the compiler does not support
them). That's just evolution of tools. For example, what debugger is
used to debug a debugger?

V
 
N

Noah Roberts

Victor said:
Greg said:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first translated
C++ source code into C and then compiled the translated sources with
a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are pretty
much the same thing. So, how could it have been written in C++?

Something like C compilers maybe. Start with a machine code assembler.
Assemble stage1 compiler. Compile stage 2....etc...until you
eventually compile your actual compiler.
 
G

Greg

Victor said:
Greg said:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first translated
C++ source code into C and then compiled the translated sources with
a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are pretty
much the same thing. So, how could it have been written in C++? What
would be used to translate its own code from C++ "into C"? And if it
wasn't used itself, what was used? And if, in fact, some other thing
was used, what do you call it if not "C++ translator"? And what was
that thing written in? Catch my drift? There is no "chicken and egg"
problem. The "chicken and egg" conundrum exists when there is circular
dependency. There is none here. First C++ compiler/translator was
most likely written in C (or C With Classes or some other predecessor
of the "real C++"). After than each next version of a C++ compiler is
written in C++ probably (because the C++ compiler programmers know C++
the best), and any new advanced features the next version implements
are simply not used to write it (because the compiler does not support
them). That's just evolution of tools. For example, what debugger is
used to debug a debugger?

The first C++ compiler was written in C++. It compiled itself. From
Wikipedia:

"As Cfront was written in C++, it was a challenge to bootstrap on a
machine without a C++ compiler/translator. Along with the Cfront C++
sources, a special "half-preprocessed" version of the C code resulting
from compiling Cfront with itself was also provided. This C code was to
be compiled with the native C compiler, and the resulting executable
could then be used to compile the Cfront C++ sources."

Greg
 
N

Noah Roberts

Greg said:
Victor said:
Greg said:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first translated
C++ source code into C and then compiled the translated sources with
a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are pretty
much the same thing. So, how could it have been written in C++? What
would be used to translate its own code from C++ "into C"? And if it
wasn't used itself, what was used? And if, in fact, some other thing
was used, what do you call it if not "C++ translator"? And what was
that thing written in? Catch my drift? There is no "chicken and egg"
problem. The "chicken and egg" conundrum exists when there is circular
dependency. There is none here. First C++ compiler/translator was
most likely written in C (or C With Classes or some other predecessor
of the "real C++"). After than each next version of a C++ compiler is
written in C++ probably (because the C++ compiler programmers know C++
the best), and any new advanced features the next version implements
are simply not used to write it (because the compiler does not support
them). That's just evolution of tools. For example, what debugger is
used to debug a debugger?

The first C++ compiler was written in C++. It compiled itself. From
Wikipedia:

"As Cfront was written in C++, it was a challenge to bootstrap on a
machine without a C++ compiler/translator. Along with the Cfront C++
sources, a special "half-preprocessed" version of the C code resulting
from compiling Cfront with itself was also provided.

So, how was the CFront that compiled the half processesd CFront code
used to compile CFront compiled?
 
V

Victor Bazarov

Greg said:
Victor said:
Greg said:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first
translated C++ source code into C and then compiled the translated
sources with a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are
pretty much the same thing. So, how could it have been written in
C++? What would be used to translate its own code from C++ "into
C"? And if it wasn't used itself, what was used? And if, in fact,
some other thing was used, what do you call it if not "C++
translator"? And what was that thing written in? Catch my drift?
There is no "chicken and egg" problem. The "chicken and egg"
conundrum exists when there is circular dependency. There is none
here. First C++ compiler/translator was most likely written in C
(or C With Classes or some other predecessor of the "real C++").
After than each next version of a C++ compiler is written in C++
probably (because the C++ compiler programmers know C++ the best),
and any new advanced features the next version implements are simply
not used to write it (because the compiler does not support them).
That's just evolution of tools. For example, what debugger is used
to debug a debugger?

The first C++ compiler was written in C++. It compiled itself. From
Wikipedia:

"As Cfront was written in C++, it was a challenge to bootstrap on a
machine without a C++ compiler/translator. Along with the Cfront C++
sources,

....let's call them 'a'..
a special "half-preprocessed" version of the C code

....let's call it 'b'...
resulting
from compiling Cfront with itself

[huh! very interesting!]
was also provided. This C code was
to be compiled with the native C compiler, and the resulting
executable could then be used to compile the Cfront C++ sources."

I wasn't aware (although I now think it was unfounded) that Wikipedia
contains such convoluted and misleading "explanations".

How could Cfront compile itself?

struct Cfront {
CfrontSources a;
HalfProcessedC b;

Cfront() : a(typed_up()), b(Cfont().compile(Cfront())) {}
};

Take a look at the constructor initialiser list. Notice the recursion?
It's infinite!

V
 
K

Kai-Uwe Bux

Victor said:
Greg said:
Victor said:
Greg wrote:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first
translated C++ source code into C and then compiled the translated
sources with a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are
pretty much the same thing. So, how could it have been written in
C++? What would be used to translate its own code from C++ "into
C"? And if it wasn't used itself, what was used? And if, in fact,
some other thing was used, what do you call it if not "C++
translator"? And what was that thing written in? Catch my drift?
There is no "chicken and egg" problem. The "chicken and egg"
conundrum exists when there is circular dependency. There is none
here. First C++ compiler/translator was most likely written in C
(or C With Classes or some other predecessor of the "real C++").
After than each next version of a C++ compiler is written in C++
probably (because the C++ compiler programmers know C++ the best),
and any new advanced features the next version implements are simply
not used to write it (because the compiler does not support them).
That's just evolution of tools. For example, what debugger is used
to debug a debugger?

The first C++ compiler was written in C++. It compiled itself. From
Wikipedia:

"As Cfront was written in C++, it was a challenge to bootstrap on a
machine without a C++ compiler/translator. Along with the Cfront C++
sources,

...let's call them 'a'..
a special "half-preprocessed" version of the C code

...let's call it 'b'...
resulting
from compiling Cfront with itself

[huh! very interesting!]
was also provided. This C code was
to be compiled with the native C compiler, and the resulting
executable could then be used to compile the Cfront C++ sources."

I wasn't aware (although I now think it was unfounded) that Wikipedia
contains such convoluted and misleading "explanations".

How could Cfront compile itself?

In principle, this is very easy: just run the first installation of Cfront
on hardware that runs C++ natively, e.g., employ a skilled human.


Best

Kai-Uwe Bux
 
M

Markus Schoder

Victor said:
Greg said:
Victor said:
Greg wrote:
[...]
Actually the first C++ compiler was itself written in C++ - which
might sound like a chicken-and-egg impossibility were it not for one
fact: the first C++ compiler (CFront) did not actually compile C++
source code (including its own) directly - rather it first
translated C++ source code into C and then compiled the translated
sources with a C compiler. Pretty simple, isn't it?

I think you're confused. "Compile C++" and "translate C++" are
pretty much the same thing. So, how could it have been written in
C++? What would be used to translate its own code from C++ "into
C"? And if it wasn't used itself, what was used? And if, in fact,
some other thing was used, what do you call it if not "C++
translator"? And what was that thing written in? Catch my drift?
There is no "chicken and egg" problem. The "chicken and egg"
conundrum exists when there is circular dependency. There is none
here. First C++ compiler/translator was most likely written in C
(or C With Classes or some other predecessor of the "real C++").
After than each next version of a C++ compiler is written in C++
probably (because the C++ compiler programmers know C++ the best),
and any new advanced features the next version implements are simply
not used to write it (because the compiler does not support them).
That's just evolution of tools. For example, what debugger is used
to debug a debugger?

The first C++ compiler was written in C++. It compiled itself. From
Wikipedia:

"As Cfront was written in C++, it was a challenge to bootstrap on a
machine without a C++ compiler/translator. Along with the Cfront C++
sources,

...let's call them 'a'..
a special "half-preprocessed" version of the C code

...let's call it 'b'...
resulting
from compiling Cfront with itself

[huh! very interesting!]
was also provided. This C code was
to be compiled with the native C compiler, and the resulting
executable could then be used to compile the Cfront C++ sources."

I wasn't aware (although I now think it was unfounded) that Wikipedia
contains such convoluted and misleading "explanations".

How could Cfront compile itself?

struct Cfront {
CfrontSources a;
HalfProcessedC b;

Cfront() : a(typed_up()), b(Cfont().compile(Cfront())) {}
};

Take a look at the constructor initialiser list. Notice the recursion?
It's infinite!

The very first version of Cfront was probably written in plain C. It
would just add one C with classes feature. The next version would
already make use of this one feature to implement the next feature.
Rinse repeat... This is called bootstrapping.

Eventually you end up with a fully fledged C++ compiler that compiles
itself.

What Wikipedia describes is not the bootstrapping process but what was
delivered to people so they could compile their own Cfront compiler.
The half preprocessed stuff would probably compile into a Cfront
version that would not implement all features but enough to compile the
Cfront C++ source.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top