A few beginner's questions

This is a discussion on A few beginner's questions within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia You can't have that because a) A references B with pass-by-value which is defined later. b) ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,961
    Quote Originally Posted by Elysia
    You can't have that because
    a) A references B with pass-by-value which is defined later.
    b) B relies on A, which creates a circular reference.
    But A does not store an object of B, so the definition of B is only needed when defining A::doNothing. C_ntua's point is that that is one example where defining the member function inline will not work, but defining it outside of the class definition will work, assuming that a forward declaration of B is used before defining A. Admittedly, I did not consider this possibility.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    This actually works then? Standard is OK with it & all that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,961
    Quote Originally Posted by Elysia
    This actually works then? Standard is OK with it & all that?
    Yes. "A class type T must be complete if: (...) a function with a return type or argument type of type T is defined or called" (C++03 section 3.2). Thus, if a function that takes an argument of class type B is only declared, the class B need not be defined at that point.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    A class type T must be complete if: (...) a function with a return type or argument type of type T is defined (return type only?) or called.
    That line seem ambiguous with its intent.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    A class type T must be complete if: (...) a function with a return type or argument type of type T is defined (return type only?) or called.
    That line seem ambiguous with its intent.
    Not the way I read it:
    If you either DEFINE a function that returns T or has a T argument, T needs to be complete.
    OR
    If you call a function that returns T or has a T argument, T needs to be complete.

    It's just shorter to write it in the way the spec does it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,961
    How is it ambiguous?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Because it seems to contradict what you say.
    I read it as mats do. If a function uses a parameter of type T, then T must be complete.
    Of course, I interpret define as defining it in the class definition, and not as in the implementation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [QUOTE=Elysia;799547Of course, I interpret define as defining it in the class definition, and not as in the implementation.[/QUOTE]

    That's where you are going wrong. Definition of the function is what the original text refers to, and that's my intention too.

    Edit: Essentially, the code-generator needs to know how large the type is, when generating the code. Since most compilers don't read all of the code and only when it has all the code in a huge tree/list of some sort, start to generate code. Instead, each function, as it's defined, becomes some sort of "pseudo-machine code", which is then used to produce the actual machine code in some way. At this stage of "pseudo-machine code" generation, the size of all data types used by the function needs to be known.

    --
    Mats
    Last edited by matsp; 10-22-2008 at 01:12 PM.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,961
    Quote Originally Posted by Elysia
    Of course, I interpret define as defining it in the class definition, and not as in the implementation.
    To implement a function is to define it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    This is just confusing.
    A class declaration is also a class definition.
    And a class implementation is also a class definition.
    Which is which here?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    This is just confusing.
    A class declaration is also a class definition.
    And a class implementation is also a class definition.
    Which is which here?
    But we are not talking about classes, but functions. And by the way, I think a class declaration is
    Code:
    class X;
    , whilst a class defintion is
    Code:
    class X { ... stuff ... };
    - declaration tells the compiler "there's a class called X", whilst the defintion tells the compiler what is INSIDE the class (member functions and member variables).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    I think I see where the confusion stems from... Why couldn't they just make it the same for classes and functions? Silly standard-izers...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I think I see where the confusion stems from... Why couldn't they just make it the same for classes and functions? Silly standard-izers...
    But IT IS the same for classes and functions. The function declaration is a prototype - it tells the compiler "there's some function, and it takes some parameters", but nothing about what the function actually does. A function definition, like it's class counterpart, defines what is in the function (what it does).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,961
    Quote Originally Posted by Elysia
    A class declaration is also a class definition.
    And a class implementation is also a class definition.
    Which is which here?
    No, a class definition is a class declaration, but class declaration is not necessarily a class definition (e.g., a forward declaration is not a definition).

    The implementation of a class is generally considered to be the implementation of its member functions (and other related functions, like friend functions, I suppose). A function definition is the implementation of that function. A class definition contains the declarations, not necessarily definitions, of its member functions, but the member functions may also be defined inline in the class definition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by matsp View Post
    But IT IS the same for classes and functions. The function declaration is a prototype - it tells the compiler "there's some function, and it takes some parameters", but nothing about what the function actually does. A function definition, like it's class counterpart, defines what is in the function (what it does).

    --
    Mats
    But it's not the same...
    Code:
    int foo(int x);
    I would call this a declaration, never a definition.
    Similarly,
    Code:
    class foo { int foo_(int x); };
    I would call this a class definition, unlike a declaration, which is obviously should be.

    Code:
    int foo(int x) {}
    I would call this the function definition, never the function implementation.
    Similarly,
    Code:
    int foo::foo_(int x) {}
    I would call this an implementation, not a definition.

    This is exactly where the confusion arises from.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Questions
    By bjl in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2008, 05:56 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21