-1

what could be analogue code for following code snippet

    class A
    {
    };

I have above class, now I create one object pointer for it and one object for it.

    A* a;
    A aobj;

I want an analogue code for if statement below using aobj( A's object).

    if(a)
    {
    }
Apoorva sahay
  • 1,900
  • 3
  • 28
  • 45
  • 12
    I have absolutely no idea what you are asking... – tenfour Nov 03 '11 at 12:50
  • What are you trying to do? Are you trying to use 'aobj' through the 'a' pointer, or what? Can you please explain what exactly do you want to do? –  Nov 03 '11 at 12:51
  • I am tring to put object instead of pointer. I know it will not work , fo is there any way around? – Apoorva sahay Nov 03 '11 at 12:55
  • Why do you assume that the same thing can be done with `aobj`? There wouldn't be much point in having pointers if you could do the exact same things without them. – jalf Nov 03 '11 at 12:56
  • I'm with tenfour on this, but you might be after the `operator bool`. Check [safe bool idiom](http://stackoverflow.com/questions/6242296/conversion-function-for-error-checking-considered-good/6242355#6242355), though the real question is: What are you trying to achieve? – jweyrich Nov 03 '11 at 12:57
  • That makes no sense at all. `if(a)` checks if `a` points to anything or not. `aobj` is the name of an actual object that already exists, so there's nothing comparable to check! – Kerrek SB Nov 03 '11 at 12:59
  • Seriously need some education about fundamentals. read this first and still if you've doubts, please do let us know http://www.cplusplus.com/doc/tutorial/pointers/ – sarat Nov 03 '11 at 13:10

4 Answers4

3

aobj i allocated on the heap so it cannot be deleted(desctructed). It will be available as long as it's inside the scope. Once it's out of scope is overridden automatically so there is no point to use if(aobj) it will live inside the scope.

int fun() {
  A aobj;  // this object cannot be deleted because there is no new operator
          // and it will live until the function will return(or die)
  if(aobj) { // there is no point to test it because will always be true(except OS crash)
  }
 return 0;
}

If you need objects outside the scope you have to create them with new and assign null to pointers(c++11) when delete them so you will know when a object no longer exists by it's null pointer.

6D65
  • 775
  • 6
  • 14
2
if(true)
{
}

or even simpler:

;

closed as not a real answer by larsmans, jweyrich, Sam Miller, Soner Gönül, iammilind 19 mins ago, because they can.

It's difficult to tell what is being said here. This answer is ambiguous, vague, incomplete and cannot be reasonably down-voted in its current form. See the FAQ.

Eddy Pronk
  • 6,527
  • 5
  • 33
  • 57
1

There's no such thing because aobj will always be an object. It always "exists". The pointer a can be NULL, but not the object aobj.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
1

Presumably A has a constructor that initializes its members. You could add an

bool IsValid()  

member that just check if members are at default values. Eg

class A {
    int value;
    A() : value(-1) { }
    bool IsValid() { return value!=-1; }
}

if (aobj.IsValid()) {
    ...
}
Ricibob
  • 7,505
  • 5
  • 46
  • 65
  • 1
    Or provide both `bool operator bool() const` and `bool operator!() const`. – jweyrich Nov 03 '11 at 13:01
  • As other have pointed out its not really clear what you want here - Im just taking a guess at what you might getting at... – Ricibob Nov 03 '11 at 13:02
  • Validating a pointer is matter of checking against null. why we've to take so much pain. I never have wrote a single line of code like this – sarat Nov 03 '11 at 13:14
  • @jweyrich: It's usually not a good idea to provide `operator bool()`, since that makes the class convertible to any numeric type, which can cause ambiguity and confusion. In C++11, provide `explicit operator bool()`; in C++03, use the ["safe bool" idiom](http://www.artima.com/cppsource/safebool.html). – Mike Seymour Nov 03 '11 at 14:18