Thread: A few beginner's questions

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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
    28,413
    How is it ambiguous?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

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, 06: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, 09: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