with

This is a discussion on with within the C++ Programming forums, part of the General Programming Boards category; Does C++ have a 'with' key word as in Java? The 'with' key word is used to avoid repetition of ...

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking with

    Does C++ have a 'with' key word as in Java?
    The 'with' key word is used to avoid repetition of object name ???
    I Java, some thing like :
    Code:
    obj.get_data();
    if(obj.check()) obj.rectify();
    obj.display();
    obj.reset();
    could be simplified with, the use of 'with' as :
    Code:
    with obj{
      get_data();
      if(check()) rectify();
      display();
      reset();
    }
    I know that the introduction of such a keyword if it doesn't exist will cause scope prblms.
    But it seems really helpful in Java.
    P.S. I Don't know Java very well;
    neither have I programmed with it.

    (^_^)!
    .

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by arjunajay
    Does C++ have a 'with' key word as in Java?
    No.
    Quote Originally Posted by arjunajay
    I know that the introduction of such a keyword if it doesn't exist will cause scope prblms.
    That is one of several reasons it is not in C++; C++ supports multiple inheritence of implementation, so there are a lot of cases where the "with" logic would simply not work, or be ambiguous.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Java doesn't have a with keyword...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I have sort of imitated that behavior by having the methods return a reference to the object so that you can then chain them together like
    Code:
    obj& obj::getdata()
    {
        //do stuff
        return *this;
    }
    //same for other functions
    
    obj.getdata()
         .display()
         .reset();
    Ofcourse, the "if" statement from your example can't be easiliy integrated into this but the others work

  5. #5
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking Oops?

    Quote Originally Posted by CornedBee
    Java doesn't have a with keyword...
    It doesn't? (I did give you fair warning. (^_^)).
    Let me refer the book once more and try to give you the actual code given in the book. (If I can find it again in the library).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It doesn't. I think you're confusing it with JavaScript, which does have the with keyword.

    Quote Originally Posted by The Java Language Specification
    3.9 Keywords
    The following character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (§3.8):

    Keyword: one of
    Code:
    	abstract    default    if            private      this
    	boolean     do         implements    protected    throw
    	break       double     import        public       throws
    	byte        else       instanceof    return       transient
    	case        extends    int           short        try
    	catch       final      interface     static       void
    	char        finally    long          strictfp     volatile
    	class       float      native        super        while
    	const       for        new           switch
    	continue    goto       package       synchronized
    As you can see, "with" is not there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Perhaps you were thinking of VB?
    Code:
    With...End With allows you to perform a series of statements on a specified object
    without requalifying the name of the object. For example, to change a number of
    different properties on a single object, place the property assignment statements
    within the With...End With, referring to the object once instead of referring to it
    with each property assignment. The following example illustrates use of
    With...End With to assign values to several properties of the same object:
    
    With MyLabel
       .Height = 2000
       .Width = 2000
       .Text = "This is MyLabel"
    End With
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    IIRC, some versions of Pascal and related languages have the "with" keyword which functions as described.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Pascal has a 'with' keyword.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    I think it could have been Java script...
    But certainly noy Pascal or VB.
    More over I would have mistaken the beginer's book in javascript for java.
    Any wayI can't find the book again...
    But I still ask why that seems a good help in the languages specified, but not implemented in C++?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    They don't like adding new keywords to the language, and you can get similar functionality without it. Just make a reference variable with a single letter name and it will be close enough without the ambiguity.

Popular pages Recent additions subscribe to a feed

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