CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    2 basic question

    Hi,

    I have 2 basics (or at least I think they are) C++ question

    1. What is the difference meaning of the const location?
    a. static const char* x = "abc"
    b. const static char* y = "xyz"

    2. What is the benift/when should I use forward declaration instead of include?

    Thanks in advance
    Avi

  2. #2
    Join Date
    Jan 2007
    Posts
    69

    Re: 2 basic question

    The static declaration of the variable means it can be only used in that source file. Or in the case of a member variable, it is considered a global for that class.

    Forward declaration only allows you to use that type in all function types (constructors, member functions, operators, etc....) and declare a variable pointer to that type of object. (although for the source file where the implementation of those functions are, you need to #include the correct file for it's definition)

    You cannot declare a 'member variable' that isn't a pointer in a class without knowing it's full definition. Because without it's full definition, you do not know the 'sizeof' the object.

    Example:

    The following works.

    Code:
    #include <iostream>
    
    class B; // forward declare
    
    class A
    {
    public:
    	A() {}
    	~A() {}
    private:
    	B * ObjectPointer;
    };
    
    class B
    {
    public:
    	B() {}
    	~B() {}
    };
    
    int main(int count,char * args[])
    {
    	return 0;
    }
    The following does not.

    Code:
    #include <iostream>
    
    class B; // forward declare
    
    class A
    {
    public:
    	A() {}
    	~A() {}
    private:
    	B Object;
    };
    
    class B
    {
    public:
    	B() {}
    	~B() {}
    };
    
    int main(int count,char * args[])
    {
    	return 0;
    }
    The error reported is:

    'A::Object' uses undefined class 'B'

    The reason is because class B is not defined before A is. So A has no idea of what a B is. It just knows it exists because of forward declaration.

    The solution here is to define class B before class A.

    Code:
    class B
    {
    public:
        B() {}
        ~B() {}
    };
    
    class A
    {
    public:
        A() {}
        ~A() {}
    private:
        B Object;
    };
    In the following example, the A object interface has no idea what a B is, but the implementation does, so it works.

    Code:
    #include <iostream>
    
    class B;
    
    class A
    {
    public:
    	A(const B &);
    	~A();
    private:
    	int Value;
    };
    
    class B
    {
    public:
    	B(int value);
    	~B();
    	int GetValue() const;
    private:
    	int OurValue;
    };
    
    int main(int count,char * args[])
    {
    	B obj(100);
    	A obj2(obj);
    
    	return 0;
    }
    
    A::A(const B & ex) : Value(ex.GetValue())
    {
    }
    
    A::~A()
    {
    }
    
    B::B(int value) : OurValue(value)
    {
    }
    
    B::~B()
    {
    }
    
    int B::GetValue() const
    {
    	return OurValue;
    }
    This is the example of forward declaration for that type being able to be used in interface 'functions'. And it would still work even if the A constructor was designed to take a B object, and not a constant reference to a B object. And that's because it's the implementation of the function that needs to know the size of the B object, and not the interface.
    Last edited by James2007; February 4th, 2007 at 05:05 PM.

  3. #3
    Join Date
    Sep 2003
    Posts
    815

    Re: 2 basic question

    I still do not understnad what is the different meaning of the const location:

    1. What is the difference meaning of the const location?
    a. static const char* x = "abc"
    b. const static char* y = "xyz"

    I think 1 of them means that the pointer is const and the other that the pointer value (whe value it points to) is const but I'm not sure, and I do not know which 1 is which...

    Thanks again
    Avi

  4. #4
    Join Date
    Dec 2006
    Posts
    33

    Re: 2 basic question

    I don't think there's a difference between the two lines. "static" and "const" each have their own meanings which don't interact. If you are asking the difference between:

    1. const char * - a pointer to an unchangeable character
    2. char const *- same as 1, const attaches to the left
    3. char * const - a unchangeable pointer to a changeable character

    Hope that helps!

  5. #5
    Join Date
    Sep 2003
    Posts
    815

    Re: 2 basic question

    Thanks

    That's exactly what I have needed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured