[need help] Initializing Objects in Control Structures


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: [need help] Initializing Objects in Control Structures

  1. #1
    Join Date
    Oct 2008
    Posts
    2

    [need help] Initializing Objects in Control Structures

    I'm having trouble initializing Objects while they're inside if else statements. I'm using Visual C++ 2008.

    for instance:
    Code:
    class Account {
      Account();
      Account(int = idnum, double creditLimit = 500.00);
      Account(double initBal, int idnum, double creditLimit = 100.00);
    public:
      somefunction();
    };
    
    if (a > 0 && b > 0) {
      Account customer(a,b);
    } else {
      Account customer;
    }
    
    customer.somefunction();
    it would me this error:
    error C2065: 'customer' : undeclared identifier
    error C2228: left of '.somefunction' must have class/struct/union
    i know that this means, i just don't know if there's a better way to do this.

    i'm required to take account information and initialize the object depending on the user's input.

    i was thinking i'd use if else statements then create the object, but apparently that doesn't work.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Your object declarations are restricted to the scope of their blocks:

    if (a > 0 && b > 0) {
    Account customer(a,b); //visible only until the }
    }
    else {
    Account customer; //same here
    }

    If you want a single object that changes its state according the if-condition, declare that object outside:
    Code:
    Account customer;
    
    if (a > 0 && b > 0) {
      customer= Account(a,b);
    } else {
      //the else isn't needed any more! customer is default initialized anyway
    }
    
    //now use account
    Of course, you can use a more elegant solution: define a member function that changes the values of idnum and creditLimit instead of relying on the constructor.
    Danny Kalev

Similar Threads

  1. what is difference Objects and Control ?
    By vnkh in forum ASP.NET
    Replies: 1
    Last Post: 05-07-2002, 02:00 AM
  2. OO Considered Harmful
    By Richard Dalton . in forum Architecture and Design
    Replies: 33
    Last Post: 07-30-2001, 02:11 AM
  3. Replies: 0
    Last Post: 07-04-2001, 03:21 AM
  4. Control objects in different frames
    By Thomas in forum Web
    Replies: 0
    Last Post: 05-03-2001, 04:08 AM
  5. WEIRD ActiveX control problem
    By Glenn in forum ASP.NET
    Replies: 0
    Last Post: 12-13-2000, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center