why must blocks be used when initializing in a switch?

This is a discussion on why must blocks be used when initializing in a switch? within the C++ Programming forums, part of the General Programming Boards category; why do I need the braces ({}) in the following segment? When I tkae them away I get this error ...

  1. #1
    Shadow12345
    Guest

    why must blocks be used when initializing in a switch?

    why do I need the braces ({}) in the following segment? When I tkae them away I get this error

    Code:
    #include "Main.h"
    #include "Node.h"
    #include "Data.h"
    #include "InternalNode.h"
    
    InternalNode::InternalNode(Data * dat, Node * next):
    myData(dat), myNext(next)
    {
    }
    
    Node * InternalNode::Insert(Data& dat) {
    	
    	int result = myData->Compare(dat);
    	switch(result) {
    	case Smaller:
    	case Equal:
    		{Node * newNode = new InternalNode(&dat, this);return newNode;}
    	
    	break;
    	case Bigger:
    		myNext = myNext->Insert(dat);
    		return this;	
    		break;
    	default:
    		break;
    	}
    
    	return 0;
    }
    EDIT: Also is the way I passed in the reference to the constructor appropriate? I know there are better ways to do it, but I want this way for now as long as it will work

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: why must blocks be used when initializing in a switch?

    Originally posted by Shadow12345
    why do I need the braces ({}) in the following segment? When I tkae them away I get
    How about because that's why the C language was written? Besides, there are like five {} blocks in that code. Which one are you talking about removing?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: why must blocks be used when initializing in a switch?

    Originally posted by Shadow12345
    why do I need the braces ({}) in the following segment? When I tkae them away I get this error

    Code:
    #include "Main.h"
    #include "Node.h"
    #include "Data.h"
    #include "InternalNode.h"
    
    InternalNode::InternalNode(Data * dat, Node * next):
    myData(dat), myNext(next)
    {
    }
    
    Node * InternalNode::Insert(Data& dat) {
    	
    	int result = myData->Compare(dat);
    	switch(result) {
    	case Smaller:
    	case Equal:
    		{Node * newNode = new InternalNode(&dat, this);return newNode;}
    	
    	break;
    	case Bigger:
    		myNext = myNext->Insert(dat);
    		return this;	
    		break;
    	default:
    		break;
    	}
    
    	return 0;
    }
    EDIT: Also is the way I passed in the reference to the constructor appropriate? I know there are better ways to do it, but I want this way for now as long as it will work
    as the MS explination says, unless it is in braces, newNode is in scope for the entire switch statement. and if that is the case, it will be uninitialized in the other blocks (besides the case Smaller and case Equal)
    hello, internet!

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    The only reason I can think of is that if the braces werent required, newNode would have a scope consisting of the entire switch statement but would only be declared in a particular case, hence it may not have been declared when certain cases are executed, therefore it is necessary to define a scope restricted to the case newNode is declared in

    edit: I guess I should have read moi's response
    Last edited by *ClownPimp*; 12-26-2002 at 09:11 PM.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  5. #5
    Shadow12345
    Guest
    as the MS explination says, unless it is in braces, newNode is in scope for the entire switch statement. and if that is the case, it will be uninitialized in the other blocks (besides the case Smaller and case Equal)
    But I want it to be uninitialized in the other blocks, I don't see what is wrong with that.

    The only reason I can think of is that if the braces werent required, newNode would have a scope consisting of the entire switch statement but would only be declared in a particular case, hence it may not have been declared when certain cases are executed, therefore it is necessary to define a scope restricted to the case newNode is declared in
    I don't know this whole thing only somewhat makes sense. What is wrong with newNode having a scope consisting of the entire switch but only being declared in a particular case? I guess I don't see it.

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    then do this:

    Code:
    #include "Main.h"
    #include "Node.h"
    #include "Data.h"
    #include "InternalNode.h"
    
    InternalNode::InternalNode(Data * dat, Node * next):
    myData(dat), myNext(next)
    {
    }
    
    Node * InternalNode::Insert(Data& dat) {
    	
    	int result = myData->Compare(dat);
    	Node *newNode;
    	switch(result) {
    	case Smaller:
    	case Equal:
    		newNode = new InternalNode(&dat, this);
    		return newNode;
    	
    	break;
    	case Bigger:
    		myNext = myNext->Insert(dat);
    		return this;	
    		break;
    	default:
    		break;
    	}
    
    	return 0;
    }
    hello, internet!

  7. #7
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    or this

    Code:
    int main(void)
    {
       ...
       {
          Node * newNode = new InternalNode(&dat, this);      
          switch (result)
          {
             ...
          }
       }

  8. #8
    Shadow12345
    Guest
    well that pretty much sums everything up. The pathetic thing is I am going to make this mistake about 3 more times before I actually learn it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21