Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what is wrong with this code?

what is wrong with this code?

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sanket patel
    wrote on last edited by
    #1

    Hello All ? can anyone please tell me what's wrong with following code?

    class A
    {
    public:
    	A(const string& s)
    	{ /* */}
    
    	string f()
    	{
    		return "hello world";
    	}
    
    };
    
    Class B : public A
    {
    private:
    string s;
    public:
    
    	B():A(s=f())
    	{ }
    
    
    };
    
    void main()
    {
    B obB;
    }
    

    Thank you,

    sanket patel

    D N 2 Replies Last reply
    0
    • S sanket patel

      Hello All ? can anyone please tell me what's wrong with following code?

      class A
      {
      public:
      	A(const string& s)
      	{ /* */}
      
      	string f()
      	{
      		return "hello world";
      	}
      
      };
      
      Class B : public A
      {
      private:
      string s;
      public:
      
      	B():A(s=f())
      	{ }
      
      
      };
      
      void main()
      {
      B obB;
      }
      

      Thank you,

      sanket patel

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Is this a quiz? Why don't you first tell us what problem you are having.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      S 1 Reply Last reply
      0
      • D David Crow

        Is this a quiz? Why don't you first tell us what problem you are having.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        S Offline
        S Offline
        sanket patel
        wrote on last edited by
        #3

        oh I am sorry! whenever I try to run this code my program crashes. I dont know what is wrong with it?

        sanket patel

        D 1 Reply Last reply
        0
        • S sanket patel

          oh I am sorry! whenever I try to run this code my program crashes. I dont know what is wrong with it?

          sanket patel

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          sanket.patel wrote:

          ...my program crashes.

          Where? The term "crash" rarely means anything useful.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          S 1 Reply Last reply
          0
          • D David Crow

            sanket.patel wrote:

            ...my program crashes.

            Where? The term "crash" rarely means anything useful.


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            S Offline
            S Offline
            sanket patel
            wrote on last edited by
            #5

            I am sorry again!! But I dont know why this code is not executing. I know there is some problem with my understanding of the code. so I just want to know that Where I am wrong? Thank you

            sanket patel

            D 1 Reply Last reply
            0
            • S sanket patel

              I am sorry again!! But I dont know why this code is not executing. I know there is some problem with my understanding of the code. so I just want to know that Where I am wrong? Thank you

              sanket patel

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Have you tried:

              class A
              {
              public:
              A() {}
              A(const string& s) {}

              string f()	
              {		
                  return "hello world";	
              }
              

              };

              class B : public A
              {
              private:
              string s;
              public:
              B()
              {
              s = f();
              }
              };


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              S 1 Reply Last reply
              0
              • S sanket patel

                Hello All ? can anyone please tell me what's wrong with following code?

                class A
                {
                public:
                	A(const string& s)
                	{ /* */}
                
                	string f()
                	{
                		return "hello world";
                	}
                
                };
                
                Class B : public A
                {
                private:
                string s;
                public:
                
                	B():A(s=f())
                	{ }
                
                
                };
                
                void main()
                {
                B obB;
                }
                

                Thank you,

                sanket patel

                N Offline
                N Offline
                NealAB
                wrote on last edited by
                #7

                In class A, does it still crash if you make the f() method static? You are using A's method before A is ctor'ed. It looks like it shouldn't matter, but who knows, maybe some static consts are not available until after the first inst of the class is created? Sounds doubtful, doesn't it.

                S 1 Reply Last reply
                0
                • D David Crow

                  Have you tried:

                  class A
                  {
                  public:
                  A() {}
                  A(const string& s) {}

                  string f()	
                  {		
                      return "hello world";	
                  }
                  

                  };

                  class B : public A
                  {
                  private:
                  string s;
                  public:
                  B()
                  {
                  s = f();
                  }
                  };


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  S Offline
                  S Offline
                  sanket patel
                  wrote on last edited by
                  #8

                  Thank you David!! the code you show is working fine! so can you please explain me where I was wrong ?

                  sanket patel

                  1 Reply Last reply
                  0
                  • N NealAB

                    In class A, does it still crash if you make the f() method static? You are using A's method before A is ctor'ed. It looks like it shouldn't matter, but who knows, maybe some static consts are not available until after the first inst of the class is created? Sounds doubtful, doesn't it.

                    S Offline
                    S Offline
                    sanket patel
                    wrote on last edited by
                    #9

                    Thanks NealAB, Yeah I got your point! I think that is only happening. I was trying to use A's method before it was created. Thanks again...

                    sanket patel

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups