Pointer initialisation

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Pointer initialisation

Foster, Gareth
Hello all,

The following code compiles fine under g++/mingw, but under vc++ 6 there are
errors:


class something
{
};

class example
{
public:
        something * m_p_something;

        somemethod()
        {
                something * p_something(m_p_something); // here cv++
complains

                // etc
        }
};


At the line specified, vc++ compains that m_p_something is undeclared
amongst other things. Is this legal C++ or is it just that g++/mingw
tolerates it?

Many thanks,

Gaz


**********************************************
* http      -   http://planetearthworm.com/  *
* mail home -  [hidden email] *
* mail work -    [hidden email]   *
**********************************************


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Pointer initialisation

Greg Chicares
On 2005-06-15 11:36 AM, Foster, Gareth wrote:

>
> The following code compiles fine under g++/mingw, but under vc++ 6
> there are errors:
>
> class something
> {
> };
>
> class example
> {
> public:
> something * m_p_something;
>
> somemethod()

What return type did you intend? C++ has no 'implicit int'.

Does g++ really not say something like the following?
  "error: ISO C++ forbids declaration of `somemethod' with no type"
What version of g++ are you using? with what command-line options?
I get the above error even with gcc-2.95.2-1 and no options.

> {
> something * p_something(m_p_something); // here cv++
> complains
>
> // etc
> }
> };
>
> At the line specified, vc++ compains that m_p_something is undeclared
> amongst other things. Is this legal C++ or is it just that g++/mingw
> tolerates it?

Another handy resource is the online compiler here:
  http://www.comeaucomputing.com/tryitout/


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

RE: Pointer initialisation

Foster, Gareth
In reply to this post by Foster, Gareth
> What return type did you intend? C++ has no 'implicit int'.
>

It was sort of supposed to be a pseudo code example, I have based it on
actual code, it is mearly there to draw out the descrepancy between the two
compilers I mentioned.

Put another way, I quickly hacked up an example scenario, and forgot to
include the return type - probably because I had no intention of getting as
far as returning anything :)

So, without further ado ...

class something
{
};

class example
{
public:
        something * m_p_something;

        // constructor + other boilerplate ...

        int somemethod()
        {
                something * p_something(m_p_something); // here cv ++ 6
complains that m_p_something is undeclared
                                                                        //
my suspicion is that this is a misleading warning
                                                                        //
the question is - is this legal c++ or a g++/mingw 'feature'

                something * p_otherthing = m_p_something; // this
works fine on both g++/mingw and vc++ 6

                // do some stuff etc

                return 0;
        }
};

int main(int argc, char ** argv)
{
        example myExample;

        example.somemethod();

        return 0;
}


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Pointer initialisation

Alexey Borzenkov
In reply to this post by Foster, Gareth
On 6/16/05, Foster, Gareth <[hidden email]> wrote:
> > Now, when I add obvious void to somemethod, both gcc (3.4.2
> > mingw-special) and cl (version 13.10.3052) are compiling this code
> > with no problems. Maybe the snippet you provided is incomplete?
> You were exactly right to add the return, as I explained in a reply to
> another post on this, I only inteded the code as a quick example.
>
> That aside, it appears that code compiles fine under g++ and newer
> incarnations of vc ++ from what you have said? Can I safely assume that this
> is then legal c++ and put it down to yet another vc++ quirk do you think?

Hmm. I'm not sure. You can try something like this:

[...]
typedef something* psomething;

class example {
public:
  psomething m_p_something;

  void somemethod() {
    psomething p_something(m_p_something);
  }
};
[...]

And see if vc++ compiles that...


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. <a href="http://ads.osdn.com/?ad_idt77&alloc_id492&op=click">http://ads.osdn.com/?ad_idt77&alloc_id492&op=click
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Pointer initialisation

Greg Chicares
In reply to this post by Foster, Gareth
On 2005-06-16 04:21 AM, Foster, Gareth wrote:
>
[reformatted]

> class something
> {
> };
>
> class example
> {
> public:
>     something * m_p_something;
>
>     int somemethod()
>     {
>         // here cv ++ 6 complains that m_p_something is undeclared
>         // my suspicion is that this is a misleading warning
>         // the question is - is this legal c++ or a g++/mingw 'feature'
>         something * p_something(m_p_something);

This is all right: it declares and defines a variable.
I don't know why your other compiler chokes on it.
It works with g++, comeau, and borland.

>         // this works fine on both g++/mingw and vc++ 6
>         something * p_otherthing = m_p_something;
>
>         return 0;
>     }
> };
>
> int main(int argc, char ** argv)
> {
>     example myExample;
>
>     example.somemethod();

You meant:
    myExample.somemethod();

>     return 0;
> }


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Pointer initialisation

Chan Kar Heng

my guess is that it's a bug in VC. :)

effect of these 2 should be almost exactly the same..
i can't recall if they both invoke the same functions (copy constructor,
assignment operator, etc)..

   something * p_something(m_p_something);
   something * p_something=m_p_something;

you might try to put it this way instead & see if VC complains on the
first line instead of the 2nd?

   something * p_something=m_p_something;
   something * p_something(m_p_something);


rgds,

kh



Greg Chicares wrote:

> On 2005-06-16 04:21 AM, Foster, Gareth wrote:
>
> [reformatted]
>
>>class something
>>{
>>};
>>
>>class example
>>{
>>public:
>>    something * m_p_something;
>>
>>    int somemethod()
>>    {
>>        // here cv ++ 6 complains that m_p_something is undeclared
>>        // my suspicion is that this is a misleading warning
>>        // the question is - is this legal c++ or a g++/mingw 'feature'
>>        something * p_something(m_p_something);
>
>
> This is all right: it declares and defines a variable.
> I don't know why your other compiler chokes on it.
> It works with g++, comeau, and borland.
>
>
>>        // this works fine on both g++/mingw and vc++ 6
>>        something * p_otherthing = m_p_something;
>>
>>        return 0;
>>    }
>>};
>>
>>int main(int argc, char ** argv)
>>{
>>    example myExample;
>>
>>    example.somemethod();
>
>
> You meant:
>     myExample.somemethod();
>
>
>>    return 0;
>>}
>
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
> from IBM. Find simple to follow Roadmaps, straightforward articles,
> informative Webcasts and more! Get everything you need to get up to
> speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
>


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users