Author Topic: someone rumble about Forward declaration.  (Read 2203 times)

0 Members and 1 Guest are viewing this topic.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
someone rumble about Forward declaration.
« on: September 27, 2013, 02:18:01 pm »
Yes i understand the part of function prototypes and i have read a thing or two from the web but haven't gotten it, maybe because of this loud music at my neighbour's.


Can someone bubble about forward declaration with focus on Classes and their circular relationships and circular dependecies in a sense of coding a library.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: someone rumble about Forward declaration.
« Reply #1 on: September 27, 2013, 02:37:26 pm »
Well you see, altho the code execution starts from the  main function, the code gets compiled from the top to the bottom of the code. For example if two functions are calling each other, it doesnt matter which function is calling first, the other function will not be defined by the time the compiler reaches it's function call.
So prototypes are made so that the compiler can define its funtions before starting to compile.
The prototypes work as a reference to it's funtion for the compiler.
« Last Edit: September 27, 2013, 02:39:47 pm by rasenove »
My secrets have secrets...

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: someone rumble about Forward declaration.
« Reply #2 on: September 27, 2013, 02:49:32 pm »
Well you see, altho the code execution starts from the  main function, the code gets compiled from the top to the bottom of the code. For example if two functions are calling each other, it doesnt matter which function is calling first, the other function will not be defined by the time the compiler reaches it's function call.
So prototypes are made so that the compiler can define its funtions before starting to compile.
The prototypes work as a reference to it's funtion for the compiler.
Yeah, this explains it pretty well. But I'd like to note that the main() is usually called an EP (Entry point), because that's where the execution starts at.

Offline nfite

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: someone rumble about Forward declaration.
« Reply #3 on: September 27, 2013, 02:51:33 pm »
Like a function prototype, a forward declaration of a class declares its class name without it definition. Hence the name can be used with any undereferenced pointers or references before its definition. With forward declaration, you unleash the power of circular relationships between header files in your lib without going for circular dependecies that the compile really screams about.

For when you can help it, please stay away from circular dependecies between modules though those between classes can be torelable.

Hope i helped!

EDIT*: For dependecy management, check out cinclude2dot which is a perl script which analyses C/C++ code and produces a #include dependency graph, and makedep which is a C/C++ dependecy generator.
« Last Edit: September 27, 2013, 03:39:06 pm by nfite »

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: someone rumble about Forward declaration.
« Reply #4 on: September 27, 2013, 02:55:21 pm »
Well you see, altho the code execution starts from the  main function, the code gets compiled from the top to the bottom of the code. For example if two functions are calling each other, it doesnt matter which function is calling first, the other function will not be defined by the time the compiler reaches it's function call.
So prototypes are made so that the compiler can define its funtions before starting to compile.
The prototypes work as a reference to it's funtion for the compiler.
Thank you rasenove, but lets try to take this in the direction of classes. I am good with the function prototype ( its human understandable), but things get twisted when it comes to classes/modules if one is building a lib.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: someone rumble about Forward declaration.
« Reply #5 on: September 27, 2013, 03:04:59 pm »
Thank you rasenove, but lets try to take this in the direction of classes. I am good with the function prototype ( its human understandable), but things get twisted when it comes to classes/modules if one is building a lib.

Wat nfite said.

Hope i helped!

Indeed it did. But you have to post a (BIG) intro in the introductions board, (where we will welcome you) before posting anything.
Hi, by the way :p
My secrets have secrets...

Offline nfite

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: someone rumble about Forward declaration.
« Reply #6 on: September 27, 2013, 03:14:29 pm »
Indeed it did. But you have to post a (BIG) intro in the introductions board, (where we will welcome you) before posting anything.
Hi, by the way :p
OH, sorry. Am gonna rush there right now but i couldn't resist this.