Thread: forward class declarations

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need to put "extern" before functions. It works just as well without.
    And the whole "singleton" pattern seems flawed in the view of the "anti-singleton" pattern, so perhaps it's just best to avoid it altogether
    But I never really saw the need for a singleton, anyway....
    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.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Does the fact that implementing singletons in C++ is a pain in the ass weigh at all in your decision, executor?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by execute
    you were saying that some argue Singleton is an "anti-pattern", and that just didn't make much sense
    hmm... as in you have never heard of the term "anti-pattern"?

    Quote Originally Posted by execute
    But laserlight was nice enough with extremely wise experience & expertise, that she recommended using "extern".
    You have taken my statement out of context since I made no such recommendation. I merely pointed out that a global variable can exist across translation units by means of extern.

    Quote Originally Posted by execute
    So instead I could include "CCommon.h" and do:
    extern void getDLL(string dll);
    That would merely be a declaration of a free function. What I was talking about is declaring the object as extern.

    Quote Originally Posted by execute
    Well that sort of made the whole Singleton approach unnecessary. I don't really need a CCommon class, since my common functions can be accessed in multiple files now.
    I think that you are confusing the concept of a singleton with that of a class. This probably also explains why you suggested that manzoor replace a forward declaration with a singleton. The concepts are orthogonal.

    Quote Originally Posted by execute
    Having it as a singleton, doesn't do any damage, I am using one instance of it.
    That is a misconception. You should only choose to implement the singleton pattern for a class when it is certain that there can be only one instance of that class in any given program that uses the class. If not, you are doing "damage" because you are limiting the number of instances of the class when there is no good reason to do so.

    Quote Originally Posted by execute
    So I was saying in a way, I could eliminate the singleton and do the same thing, and I can also do it with singleton, it's the same.
    Talking of eliminating the singleton does not make sense. What you are talking about is the removal of the implementation of the singleton pattern.

    EDIT:
    To put things in perspective consider:
    Code:
    #ifndef SKETCHERVIEW_H_
    #define SKETCHERVIEW_H_
    
    // sketcherview.h
    
    class CElement; // Forward declaration.
    
    class CSketcherView
    {
    public:
        // ...
    
        void view() const;
    private:
        CElement* element;
    };
    
    #endif
    Code:
    //sketcherview.cpp
    #include "sketcherview.h"
    #include "element.h"
    
    // ...
    
    void CSketcherView::view() const
    {
        element->draw();
    }
    execute, what you suggested is that this be turned into:
    Code:
    #ifndef SKETCHERVIEW_H_
    #define SKETCHERVIEW_H_
    
    // sketcherview.h
    
    class CSketcherView
    {
    public:
        // ...
    
        void view() const;
    };
    
    #endif
    Code:
    //sketcherview.cpp
    #include "sketcherview.h"
    #include "element.h"
    
    // ...
    
    void CSketcherView::view() const
    {
        CElement::getInstance().draw();
    }
    I do not see how the latter can possibly be a replacement for the former when the former allows multiple CSketcherView objects to own their own CElement objects, and allows for polymorphism where CElement is an abstract base class. Clearly, forward declarations and the singleton pattern are not substitutable.
    Last edited by laserlight; 12-05-2008 at 04:13 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Forward Declaration of Class Members?
    By 691175002 in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2008, 10:34 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM