Re: newbie questions on C++

Discussion in 'C++' started by CW, May 19, 2003.

  1. CW

    CW Guest

    Hi

    Thanks for the speedy reply.

    I still need some clarifications.

    On forward declaration again:

    If I keep Class A's definition in A.h, and A's implementation in A.cpp, and
    B's definition in B.h, and B's implementation in B, where do I need to place
    forward declarations.

    I tried various permutations such as placing forward declaration of Class B
    in A.h, and forward declaration of Class A in B.h, or forward declaration of
    Class B in A.cpp and forward declaration of Class B in A.cpp. None seems to
    work.

    The only way I could get them to work to combine A.h, B.h, A.cpp, B.cpp into
    a single file.

    Is forward declaration actually possible across different files?

    Thanks again.


    "Huw Ford" <> wrote in message
    news:NY1ya.2635$...
    > "CW" <> wrote in message
    > news:...
    > > Q1:
    > > Let's say I have the following definition

    >
    > You would need to add a forward reference;
    > class B;
    >
    > >
    > > class A
    > > {
    > > public:
    > > void method(const B& b) const;

    > or the following works with VC++ 7 but I don't know if it's standard
    > void method(const class B& b) const;
    > > };
    > >
    > > class B
    > > {
    > > public:
    > > void method(const A& b) const;
    > > };
    > >
    > > Definitions of A and B happens to cross reference each other. Anyway to
    > > resolve this circular reference, and let the compiler know that

    definition
    > > of B is to follow (or possibly in another header file)?.
    > >
    > > Q2:
    > > Is there anything inherently wrong with passing instances of one class

    to
    > > another? Obviously, as in the above case, class A and class B are more
    > > tightly coupled than I would like it to be. However, should I define
    > > subclasses of B (or subclasses of A), then A::Method and B::Method

    should
    > > continue to work even if I am passing subclasses of B or subclasses of

    A.
    >
    > This is a design question and probably off topic for this newsgroup.
    > >
    > > Q3:
    > > Operator overloading. If I want to overload operator<, I notice that I

    am
    > > supposed to use friend as the qualifier.
    > >
    > > For example given the following definition
    > >
    > > class A
    > > {
    > > friend bool operator< (const A& a, const A& b);
    > > ...
    > > };
    > >
    > > friend bool operator<(const A& a, const A& b)
    > > {
    > > if ...
    > > return true;
    > > else
    > > return false;
    > > }
    > >
    > > Let's say that I could write operator< function as a member public

    > function
    > > rather than a friend, is there any reason why I should use friend

    instead
    > of
    > > a normal public member function?

    >
    > It depends if you are comparing like with like.
    > If you want to compare an instance of class A with another instance of

    class
    > A there is nothing to stop you making it a member function.
    > However if you want to compare an A to an int the member function could

    only
    > every be used in the following way;
    >
    > A a;
    > int i;
    > if (a < i) ...
    > but
    > if (i < a) ...
    > would fail
    > as you can't declare a member function < for int which takes an A as it's
    > parameter. You would need to write;
    > friend bool operator < (const int& i,const A& a) {...}
    >
    > >
    > > Q4:
    > > Let's say that A is a class
    > >
    > > I have code similar to the follows:
    > >
    > > A a;
    > > a.method();
    > > ...
    > > a=a("constructor argument");

    >
    > not valid code unless you have provided a () operator. I believe in your
    > context it would have to be
    > a=A("constructor argument");
    >
    > You are confusing the copy operator with the assignment constructor.
    >
    > A a = ("constructor argument"); // Assignment operator
    > a = A("constructor argument"); // Construct a temporary instance of A
    > then call a's copy operator
    >
    > These are two items which often seem to cause confusion for "newbies" look
    > up copy operator and assignment constructor (sometimes incorrectly in my
    > opinion called the copy constructor) in any good C++ book. (if it doesn't
    > explain the two items clearly it's not a good C++ book)
    >
    > > a.method();
    > > ...
    > >
    > > In this scenario, when a is initially declared, the automatic

    constructor
    > > would have run creating the new instance a of class A. I then

    subsequently
    > > instantiated another instance using a non-default constructor. Would the
    > > first and second instance of a occupy the same piece of memory or when I
    > > instantiated the 2nd instance, there is a memory leak of the original
    > > instance?
    > >
    > > Thanks in advance
    > >

    > Hope that helps
    >
    >
     
    CW, May 19, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ali Syed
    Replies:
    3
    Views:
    572
    Mark McIntyre
    Oct 13, 2004
  2. pkirk25
    Replies:
    50
    Views:
    1,141
    Bill Pursell
    Oct 4, 2006
  3. Replies:
    10
    Views:
    565
  4. JohnE

    newbie with newbie questions

    JohnE, Aug 17, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    522
    Gregory A. Beamer
    Aug 17, 2009
  5. Vijay
    Replies:
    2
    Views:
    609
    Öö Tiib
    May 7, 2010
Loading...

Share This Page